Java - Date constructor accepts a Date String, But deprecated. Tried alternatives, but no luck
Asked Answered
R

4

5
String temp_date="07/28/2011 11:06:37 AM";  
Date date = new Date(temp_date); //Depricated 
SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss"); 
String comp_date= sdf.format(date);
System.out.println(comp_date);

This works, But If I use something like this

String temp_date="07/28/2011 11:06:37 AM";  
try{  
    SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss"); 
    Date comp_date= sdf.parse(temp_date);
    System.out.println(comp_date);
}catch(Exception e){
    System.out.println(e);
}

This exception is thrown:

java.text.ParseException: Unparseable date: "07/28/2011 11:06:37 AM"
Retrenchment answered 29/7, 2011 at 16:27 Comment(4)
Regarding the java.util.Date being deprecated, you might want to start taking a look at the abstract java.util.Calendar class and its direct implementation: java.util.GregorianCalendarAorist
@Gevorg: Calendar is not very helpful when it comes to parsing/formatting dates from/to String. Regardless, I'd prefer Joda Time: joda-time.sourceforge.net.Universality
@Balus - I heard of Beanutils of Apache commons for DateConversion. Incase you used beanutils - For the above case, how would it look like?Retrenchment
@BalusC: yes, for parsing/formatting then DateFormat is the answer. I was suggesting GC instead of the simple Date for datetime manipulation. I'm not familiar with Joda Time, I'll look into it. ThanksAorist
U
12

Your parsing pattern is wrong. It does not match the date string representation. The MMM denotes a 3-letter localized month abbreviation, while you have 2-digit month number in your actual date, you need MM. You've also slashes / as date/month/year separator and not -. For the AM/PM marker you also need an a afterwards so that the right hh can be parsed.

This should work:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); 

For an explanation of those patterns, read the SimpleDateFormat javadoc.


I believe that your concrete functional requirement is to convert the given date string as specified by the pattern MM/dd/yyyy hh:mm:ss a into another date string format, as specified by the pattern MMM-dd-yyyy hh:mm:ss. In that case, you should then have two SimpleDateFormat instances, one which parses the string in the given pattern to a Date and another which formats the parsed Date to the given pattern. This should do what you want:

String inputDate = "07/28/2011 11:06:37 AM";
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(inputDate);
String outputDate = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").format(date);
System.out.println(outputDate); // Jul-28-2011 11:06:37

Note that I changed hh in output to be HH because it would otherwise end up in 1-12 hour representation without an AM/PM marker. The HH represents it as 0-23 hour.

Universality answered 29/7, 2011 at 16:31 Comment(0)
D
2

At first look, it looks as if your format string is wrong.

MMM -- You specified this for the month, but you aren't passing a 3 char month.

Try MM and see if that helps.

Take a look at this for some additional date format information:

http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm

Dungaree answered 29/7, 2011 at 16:32 Comment(0)
U
1

The format you gave the SimpleDateFormat uses - between the month, date, and year. Your string uses slashes.

Unmeant answered 29/7, 2011 at 16:31 Comment(0)
T
1

tl;dr

Use java.time in modern Java.

LocalDateTime.parse( 
    "07/28/2011 11:06:37 AM" , 
    DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm:ss a" ).withLocale( Locale.of( "en" , "US" ) ) ;
)

java.time

In modern Java, use java.time classes defined in JSR 310, built into Java 8+.

Define a formatting pattern to match your input string.

Use two M characters for the month number, MM, not three. Three is for name of month in text rather than month number.

Use the matching character for delimiter. In your input, that means SOLIDUS (slash) character rather than hyphen.

String input = "07/28/2011 11:06:37 AM";  
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm:ss a" ) ;

We also need a Locale to specify the human language and cultural norms to be used in parsing the AM/PM.

Locale locale = Locale.of( "en" , "US" ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm:ss a" ).withLocale( locale ) ;

Your input represents a date with time-of-day but lacks the context of a time zone or offset-from-UTC. So parse as a LocalDateTime.

Be aware that your input, and the LocalDateTime class, do not represent a moment, is not a point on the timeline. Without a zone or offset, we have no way to know if your input means 11 AM in Tokyo Japan, 11 AM in Toulouse France, or 11 AM in Toledo Ohio — three very different moments, several hours apart.

LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

See this code run at Ideone.com.

ldt.toString(): 2011-07-28T11:06:37

Tristantristas answered 11/10 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.