Efficient date parsing [duplicate] - java

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.

Related

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

How to parse date string EEE MMM dd to yyyy-MM-dd in java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
how to parse output of new Date().toString()
(4 answers)
Closed 3 years ago.
I'm having the date in this pattern EEEEE MMMMM yyyy HH:mm:ss.SSSZ, I would like to convert this to yyyy-MM-dd format in java, I tried below approach but I'm getting this exception java.text.ParseException: Unparseable date:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "Sun Oct 01 00:00:00 EDT 2017";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Can someone please help me to resolve this?
Thanks.
From java-8 you can use the ZonedDateTime with pattern of your input date which is EEE MMM dd HH:mm:ss zzz yyyy
String dateInString = "Sun Oct 01 00:00:00 EDT 2017";
ZonedDateTime time = ZonedDateTime.parse(dateInString,DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(time.toLocalDate()); //2017-10-01
By default LocalDate is without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
You've defined your formatter as the concept 'date, month, year', and then you try to ask it to parse a string that isn't in this format at all. You need to make a formatter that can format Sun Oct 01 00:00:00 EDT 2017, and dd-MMM-yyyy obviously isn't it. The javadoc of SimpleDateFormat will tell you what combination of letters you need to use.
Once you've got that, it's easy: parse with this new formatter, then call .format with your old one (the dd-MMM-yyyy one).
You can't use the same formatter for both parsing and formatting. See this answer: https://stackoverflow.com/a/999191
You double-create the DateFormat one parse and once to format
DateFormat dfParse = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
DateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd");
dfFormat.format(dfParse.parse("Sun Oct 01 00:00:00 EDT 2017"))

How to parse this date format "Wednesday, 12 June 2019 14:23:39" to this "2019-03-05T11:56:13Z" in Java? [duplicate]

This question already has answers here:
Java: How to convert string with month name to DateTime?
(1 answer)
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Closed 3 years ago.
How do I parse this date "Wednesday, 12 June 2019 14:23:39" which comes as a String to a date format like this "2019-03-05T11:56:13Z" in JAVA?
You should use the java.time API for this:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse("Wednesday, 12 June 2019 14:23:39", dateTimeFormatter);
String isoDateTime = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(isoDateTime);
which results in:
2019-06-12T14:23:39
attached example of how you can parsar your date
greetings
public static void main(String[] args) throws ParseException {
String date_s = "2011-01-18 00:00:00.0";
// *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dt.parse(date_s);
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss");
System.out.println(dt1.format(date));
}
enter image description here

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"));

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

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

Categories

Resources