Reputation: 4259
I am getting a parsing exception while I am trying the following code:
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
Exception:
Exception in thread "main" java.text.ParseException: Unparseable date: "Sat Jun 01 12:53:10 IST 2013" at com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)
Input: Sat Jun 01 12:53:10 IST 2013
Expected output: Jun 01,2013 12:53:10
How to solve this?
Upvotes: 67
Views: 391945
Reputation: 11
Your pattern is in bad format to the input String. It can be easily solved:
//init SimpleDateFormat as pattern & Locale parameter
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
//parse with your date
Date dateParser = dateFormat.parse(dayOfMonth + "." + (month) + "." + year);
Then you can easily change pattern of SimpleDateFormat (depend if you have Locale parameter)
dateFormat.applyLocalizedPattern("dd MMM, yyyy");
To cast to String:
dateObject.setText(dateFormat.format(dateParser));
Upvotes: 0
Reputation: 338496
Instead a format such as yours, use ISO 8601 standard formats for exchanging date-time values as text.
The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
Your IST
could mean Iceland Standard Time, India Standard Time, Ireland Standard Time, or others. The java.time classes are left to merely guessing, as there is no logical solution to this ambiguity.
The modern approach uses the java.time classes.
Define a formatting pattern to match your input strings.
String input = "Sat Jun 01 12:53:10 IST 2013";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
zdt.toString(): 2013-06-01T12:53:10Z[Atlantic/Reykjavik]
If your input was not intended for Iceland, you should pre-parse the string to adjust to a proper time zone name. For example, if you are certain the input was intended for India, change IST
to Asia/Kolkata
.
String input = "Sat Jun 01 12:53:10 IST 2013".replace( "IST" , "Asia/Kolkata" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
zdt.toString(): 2013-06-01T12:53:10+05:30[Asia/Kolkata]
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
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. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
Upvotes: 4
Reputation: 497
Check your Pattern (DD-MMM-YYYY) and the input for the parse("29-11-2018") method. Input to the parse method should follow : DD-MMM-YYYY i,e. 21-AUG-2019
In My Code:
String pattern = "DD-MMM-YYYY";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
startDate = simpleDateFormat.parse("29-11-2018");// here no pattern match
endDate = simpleDateFormat.parse("28-AUG-2019");// Ok
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 1
Reputation: 12767
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
This patterns does not tally with your input String which occurs the exception.
You need to use following pattern to get the work done.
E MMM dd HH:mm:ss z yyyy
Following code will help you to skip the exception.
SimpleDateFormat
is used.
String date="Sat Jun 01 12:53:10 IST 2013"; // Input String
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy"); // Existing Pattern
Date currentdate=simpleDateFormat.parse(date); // Returns Date Format,
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("MMM dd,yyyy HH:mm:ss"); // New Pattern
System.out.println(simpleDateFormat1.format(currentdate)); // Format given String to new pattern
// outputs: Jun 01,2013 12:53:10
Upvotes: 1
Reputation: 39
I needed to add a ParsePosition
expression to the parse method of class SimpleDateFormat
:
simpledateformat.parse(mydatestring, new ParsePosition(0));
Upvotes: -1
Reputation: 768
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Date currentdate=sdf.parse(date);
SimpleDateFormat sdf2=new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
System.out.println(sdf2.format(currentdate));
Upvotes: 8
Reputation: 2535
I found simple solution to get current date without any parsing error.
Calendar calendar;
calendar = Calendar.getInstance();
String customDate = "" + calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
Upvotes: 3
Reputation: 328598
Your pattern does not correspond to the input string at all... It is not surprising that it does not work. This would probably work better:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
Then to print with your required format you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
Notes:
Upvotes: 126
Reputation: 68715
Update your format to:
SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy");
Upvotes: 3
Reputation: 121998
Pattern is wrong
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
Upvotes: 6