pingu
pingu

Reputation: 695

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

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"

Upvotes: 5

Views: 15378

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 340118

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

Upvotes: 1

BalusC
BalusC

Reputation: 1109635

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.

Upvotes: 12

Jeffrey Kevin Pry
Jeffrey Kevin Pry

Reputation: 3296

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

Upvotes: 2

user684934
user684934

Reputation:

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

Upvotes: 1

Related Questions