Unable to Parse the Date, ParseException is thrown [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
Unable to parse the given date
String DT = "14 Jun 2016 09:54:02 GMT";
DateFormat simpleDateFormat = new SimpleDateFormat("dd MM yyyy HH:mm:ss z");
Date date = simpleDateFormat.parse(DT);
after this I want to convert to CST Time in this format 13-JUN-16 08.53.43
Exception StackTrace
java.text.ParseException: Unparseable date: "14-Jun-2016 09:54:02 GMT" at java.text.DateFormat.parse(Unknown Source) at package2.TimeZone.parseTime(TimeZone.java:16) at package2.TimeZone.main(TimeZone.java:10)

Date format should be like as below:
String DT = "14 Jun 2016 09:54:02 GMT";
DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yy HH:mm:ss z");
Date date = (Date) simpleDateFormat.parse(DT);
After converting date format, it should pass like below:
String newstring = new SimpleDateFormat("dd-MMMM-yy HH:mm:ss").format(date).toString().toUpperCase();
System.out.println(newstring);

To parse:
You are using the wrong mask to parse it, it should be as follows(with MMM instead of MM):
DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
Date date = simpleDateFormat.parse(DT);
To format it to the desired format:
DateFormat sdf= new SimpleDateFormat("dd-MMM-yy HH.mm.ss");
sdf.setTimeZone(TimeZone.getTimeZone("US/Central"));
String formattedDate = sdf.format(date).toUpperCase();
Without the uppercase it will show 13-jun-16 08.53.43 instead of 13-JUN-16 08.53.43

Related

Why I get a wrong result when parsing a date from string with SimpleDateFormat ? (Java) [duplicate]

This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
How does Java "week year" really work?
(2 answers)
Closed 2 years ago.
I'm trying to parse a date from string but i get a wrong date result and dont understand why :/
String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat("MMMM dd, YYYY - HH:mm", Locale.US);
Date date = parser.parse(dateStr);
When I look date result (in debugger) i see : Sun Dec 30 10:48:00 CET 2012
Can someone tell me where I'm wrong please ?
String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat(
"MMMM dd, yyyy - hh:mm", Locale.US);
Date date = parser.parse(dateStr);
System.out.println(date);
Use yyyy and not YYYY
Also don't use Date but use LocalDate and LocalDateTime.
Here is how you would do it using LocalDateTime
String dateStr = "September 6, 2013 - 10:48";
DateTimeFormatter format = DateTimeFormatter.ofPattern(
"MMMM d, y - HH:mm",Locale.US);
LocalDateTime date = LocalDateTime.parse(dateStr,format);
System.out.println(date.format(format));
Note the HH is for 24 hour time since you didn't include an AM or PM in your date string.

Java unparsable date SimpleDateFormat [duplicate]

This question already has answers here:
How to get date from date time in java
(6 answers)
Why can't this SimpleDateFormat parse this date string?
(4 answers)
how to parse output of new Date().toString()
(4 answers)
Java - Unparseable date
(3 answers)
Closed 3 years ago.
I have a date that looks like that:
Sun Dec 29 00:24:09 CET 2019
I have a little utility method that parses a string date from a format to another:
public String formatDate(String date, String fromFormat, String toFormat) throws Exception {
SimpleDateFormat from = new SimpleDateFormat(fromFormat);
SimpleDateFormat to = new SimpleDateFormat(toFormat);
return to.format(from.parse(date));
}
However, with above date format, I do not find the correct date pattern to indicate to my method.
According to SimpleDateFormat patterns documentation, it should be (if I am not mistaken), the following (for Sun Dec 29 00:24:09 CET 2019):
"E M d HH:mm:ss z yyyy"
However, it throws the following exception:
java.text.ParseException: Unparseable date: "Sun Dec 29 00:24:09 CET 2019"
at java.text.DateFormat.parse(DateFormat.java:366)
at com.aptar.simulator.Utils.formatDate(Utils.java:60)
The method is called like this:
formatDate(exDate, "E M d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");
Where
exDate = "Sun Dec 29 00:24:09 CET 2019"
Try below solution -
formatDate("Sun Dec 29 00:24:09 CET 2019","EEE MMM d HH:mm:ss z yyyy","dd-MM-yyyy HH:mm:ss");
Format should be - "EEE MMM d HH:mm:ss z yyyy"
You should use EEE for Sun and MMM for Dec
hope this helps.
Date format should be
EEE MMM dd HH:mm:ss z yyyy
Your code works fine using this format.
using java.time API
LocalDate.parse(datestr, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy")).format("TO DATE PATTERN");
Further details at Using java.time package to format date
Please find the code snippet below to solve your problem. The issue was the letter codes were correct, but there was character count mismatch , hence causing the issue. E.g.:Sun has three chars, but you were using a single E in your formatter.
public class Examp167 {
public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
SimpleDateFormat from = new SimpleDateFormat(fromFormat);
SimpleDateFormat to = new SimpleDateFormat(toFormat);
return to.format(from.parse(date));
}
public static void main(String[] args) throws Exception{
String exDate = "Sun Dec 29 00:24:09 CET 2019";
System.out.println( formatDate(exDate, "EEE MMM dd HH:mm:ss zzz yyyy", "dd-MM-yyyy HH:mm:ss"));
}
}
Firs use DateTimeFormatter instead of an old outdated class, then you should set the Locale since the day and month names are in English and last the in format needs to be MMM instead of M for the month
public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern(fromFormat, Locale.US);
DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern(toFormat, Locale.US);
return outFormatter.format(inFormatter.parse(date));
}
Example:
String exDate = "Sun Dec 29 00:24:09 CET 2019";
String out = formatDate(exDate, "E MMM d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");
System.out.println(out);
29-12-2019 00:24:09

Efficient date parsing [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 4 years ago.
I am working with system's API which returns me dates in this format -
Sun Jun 10 05:23:03 2018.
I want to efficiently parse it to something that will look like this -
"dd-mm-year".
Is there any parsing built in Java I can use? Or I need to use a specific function for it?
Thanks.
DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy");
System.out.println(LocalDate.parse(str, formatter));
Output:
2018-06-10
You can parse as below with SimpleDateFormat :
public static void main(String[] args) throws IOException, ParseException {
String str = "Sun Jun 10 05:23:03 2018";
DateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = fmt.parse(str);
System.out.println(date);
System.out.println(targetFormat.format(date));
}
Outputs :
Sun Jun 10 05:23:03 EET 2018
10-06-2018
You can use SimpleDateFormat class in Java.
Following snippet will show you how to parse date accordingly.
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String strDate= formatter.format(date);
I hope this is what you are looking for. I hope this helps you.

How to format a date in a particular format which has timezone in the output [duplicate]

This question already has answers here:
Parsing Java String into GMT Date
(5 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Java - unparseable date, need format to match "GMT-0400"
(2 answers)
Closed 5 years ago.
I have a date in string format like this. It is coming from some other souce in the below format and I cannot change that:
2025-08-08T15%3A41%3A46
I have to convert above string date in this format now:
Fri Aug 08 15:41:46 GMT-07:00 2025
So below is what I have tried:
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
Date date = dateParser.parse(decodedDate);
System.out.println(date);
And this is what it prints out on the console. It prints out PDT instead of GMT-07:00. How can I get that?
Fri Aug 08 15:41:46 PDT 2025
Also I am working with Java 7 and I can use joda-time library as well. This conversion method can be called by multiple threads.
In the desired output it is printing out GMT-07:00 so how can I get the timezone also in my code?
Update:-
How about doing this way?
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("GMT-07:00"));
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
Date date = dateParser.parse(decodedDate);
System.out.println(date.toString());
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
Date date = dateParser.parse(decodedDate);
//Decode the given date and convert to Date object
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z-07:00 yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(date)); // set the timezone and print in the desired format
Output:
Fri Aug 08 07:41:46 GMT-07:00 2025
Update: As suggected by KevinO, a better way to do is
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT-0700"));

How to resolve Unparseable date error

I am receiving date from the RSS Feed in the below format
Fri Oct 23 11:07:08 IST 2015 which i am trying to convert it into
yyyy-MM-dd HH:mm format .
I have tried this way
public class ConvertDate {
public static void main(String args[]) throws ParseException
{
String passedate = "Fri Oct 23 11:07:08 IST 2015";
String res= convertdate(passedate);
System.out.println(res);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate;
}
}
But i am getting
Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Oct 23 11:07:08 IST 2015"
at java.text.DateFormat.parse(Unknown Source)
at ConvertDate.convertdate(ConvertDate.java:20)
at ConvertDate.main(ConvertDate.java:12)
Could you please let em know how to resolve this
The date pattern does not match the input. Try change the line
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
to
SimpleDateFormat in = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Hope that helps
Your date has the format EEE MMM dd HH:mm:ss zzz yyyy with an english local.
This parses the date correctly:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)
.parse("Fri Oct 23 11:07:08 IST 2015");
Try this:
SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
in.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); //or Asia/Jerusalem
String s2 = "Fri Oct 23 11:07:08 IST 2015";
Date date = in.parse(s2);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
out.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(out.format(date));
Output:
2015-10-23 11:07
Also, note the setTimeZone. IST can either stand for Indian ST or Israel ST so it would be better if you specify which time zone you really want.
Check here for IST ambiguity.
First check your actual date which you need to work .. In my case
String day="date:10/01/2018";(In selenium need to get it from web page so i got the above string from page)
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);
When am working on this i got unparsable error....
So the day string contains some part of characters . So just remove those characters from string by using day.split(":"); String day1=day[1];
just give this day1 string in parse(); Now the updated code like as below..
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day1);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);

Categories

Resources