ParseException: Unparseable date exception
thanks.
system default locale difference.
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:mm:ss a", Locale.ENGLISH);
get solved.
String to Date Error
String : Feb 13, 2017 10:25:43 AM
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy h:mm:ss a");
or MMM d, yyyy h:mm:ss a
, MMM d, yyyy hh:mm:ss a
, MMM dd, yyyy h:mm:ss a
, MMM dd, yyyy hh:mm:ss a
, MMM dd, yyyy H:mm:ss a
, MMM dd, yyyy HH:mm:ss a
...etc
ParseException ::::: Unparseable date: "Feb 13, 2017 10:25:43 AM"
plz help.
tl;dr
LocalDateTime.parse(
"Feb 13, 2017 10:25:43 AM" ,
DateTimeFormatter.ofPattern( "MMM d, uuuu hh:mm:ss a" , Locale.US )
)
2017-02-13T10:25:43
java.time
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
Specify a formatting pattern to match your input. Note that we pass a Locale
to specify the human language and cultural norms used in translating name of month and such.
String input = "Feb 13, 2017 10:25:43 AM" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM d, uuuu hh:mm:ss a" , Locale.US ) ;
Your input string lacks any indicator of time zone or offset-from-UTC. So parse as a LocalDateTime
.
LocalDateTime ldt = LocalDateTime.parse( input , f );
ldt.toString(): 2017-02-13T10:25:43
By the way, if your input had had a comma after the year, you could have parsed using an automatically localized formatter rather than bothering to define a formatting pattern. That comma is apparently the norm for the United States (at least).
String input = "Feb 13, 2017, 10:25:43 AM" ; // With a comma after year, apparently the norm in the United States.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( Locale.US )
LocalDateTime ldt = LocalDateTime.parse( input , f );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
尝试这个。
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current Date: " + ft.format(dNow));
String CurrentTime = ft.format(dNow);
链接地址: http://www.djcxy.com/p/18610.html