1

I am working on a Spring project and I have to search Documents by date of upload. So when I pass my date as parameter of a method in DAO layer it's received like: Thu Jun 06 00:03:49 WEST 2013. And I want to format that to : 2013-06-06

I have used this code to do that but it returns 06/06/13 and other constants of DateFormat (like DateFormat.MEDIUM, ...) do not return what I am waiting for.

DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);       
System.out.println(shortDf.format(new Date())); // return 06/06/13 it's short

I have also tried the SimpleDateFormat like that:

public static Date parseDate(String date, String format)throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format,Locale.ENGLISH);
return formatter.parse(date);
}

But it is still throwing a parsing Exception:

java.text.ParseException: Unparseable date: "Thu Jun 06 00:23:33 WEST 2013"
at java.text.DateFormat.parse(DateFormat.java:337)
at TestApp.main(TestApp.java:20)
4

4 回答 4

2

This should work in your case:

    DateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");

    try {
        Date date = sourceFormat.parse("Thu Jun 06 00:23:33 WEST 2013");
        String formatted = targetFormat.format(date);
        System.out.println(formatted);
    } catch (ParseException e) {
        e.printStackTrace();
    }

First, you need to parse the date with the correct format and locale (change Locale.US to whatever suits you). The exception you were getting is caused by an incorrect parse format or missing locale.

EEE MMM dd HH:mm:ss zzz  yyyy
Thu Jun 06 00:23:33 WEST 2013

And then format the result using this formatting string:

yyyy-MM-dd
2013-06-06

(documentation)

于 2013-06-05T23:40:28.803 回答
2

Just keep in mind that SimpleDateFormat class is not thread safe and causes issues in multi-threaded environments if not used properly which means you must be really careful to use it in Spring it is multi-threaded env.

Why it is not thread-safe?

The SimpleDateFormat class mutates its internal state for formatting and parsing dates. That’s why it results issues when multiple threads use the same instance of SimpleDateFormat concurrently.

How to use SimpleDateFormat in a multi-threaded environment?

There are two options -

  • Create a new instance of SimpleDateFormat for each thread.
  • Synchronize concurrent access by multiple threads with a synchronized keyword or a lock.
于 2019-02-27T06:20:47.590 回答
1

This is as close as I can get:

DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
于 2013-06-05T23:31:04.127 回答
1

If you want to format a date to your own format like 2013-06-06, SimpleDateFormatter is a common solution. But what went wrong in your code is that you have a wrong return type for a formatted date. Here's the example:

Date d=new Date();
String formattedDate=format(d);

System.out.println("This is your date: "+formattedDate);  

public String format(String date){
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(date);
}  

To format a date into your own format, use sdf.format, not sdf.parse.
sdf.parse is used to convert String to Date, while sdf.format used to convert Date to String in a specified format.

sdf.parse returns Date, sdf.format returns String.

于 2013-06-05T23:33:00.693 回答