How to convert time string to Date in Java? ParseException [duplicate] - java

This question already has answers here:
Java unparsable date SimpleDateFormat [duplicate]
(4 answers)
Closed 2 years ago.
The time string is like Tue Feb 09 10:23:31 CST 2021, how to convert to date object?
This is my code :
SimpleDateFormat dateFormat = new SimpleDateFormat("E M d hh:mm:ss z yyyy");
Date parsedDate = dateFormat.parse("Tue Feb 09 10:23:31 CST 2021");
System.out.println("===>"+parsedDate.getTime());
It throw the below exception:
java.text.ParseException: Unparseable date: "Tue Feb 09 10:23:31 CST 2021"
at java.text.DateFormat.parse(DateFormat.java:366)

Oh I find the root cause, the SimpleDateFormat is wrong.It should be "EEE MMM dd hh:mm:ss zzz yyyy"
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
Date parsedDate = dateFormat.parse("Tue Feb 09 10:23:31 CST 2021");
System.out.println("===>"+parsedDate.getTime());
It work now.

Related

Converting date object to yyyy-MM-dd hh:mm:ss [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
SimpleDateFormatter.parse giving output in different format than specified
(1 answer)
Closed 4 years ago.
I am trying to convert this date object Fri Sep 21 08:00:00 SGT 2018 to this format yyyy-MM-dd hh:mm:ss date object.
Date dt = sd1.parse(startTime);
logger.info(dt);
logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dt.toString()));
The first log statement works, it returns me Fri Sep 21 08:00:00 SGT 2018 but the second log statement does not work.
It throws me an error
unparseable date Fri Sep 21 08:00:00 SGT 2018
What am i doing wrong here? My end goal is to get the date object in yyyy-MM-dd hh:mm:ss format.
First you have to parse the date properly with :
new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
So this should work:
Date newDate = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(newDate.getTime());
System.out.println(strDate);
But I suggest you to go with LocalDateTime
LocalDateTime ldt = LocalDateTime.parse("Fri Sep 21 08:00:00 SGT 2018", DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS")));

Formatting a string to dd/MM/yyyy [duplicate]

This question already has answers here:
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 7 years ago.
I have the string Thu Nov 12 00:00:00 GMT 2015 which is EEE MMM dd HH:mm:ss ZZZ yyyy.
I want to convert this string into a string with the format of dd/MM/yyyy, or in this case 12/11/2015.
SimpleDateFormat sFrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
SimpleDateFormat sTo = new SimpleDateFormat("dd/MM/yyyy");
String result = sTo.format(sFrom.parse("Thu Nov 12 00:00:00 GMT 2015"));

Java SimpleDateFormat parsing mystery [duplicate]

This question already has answers here:
Unparseable date
(4 answers)
Closed 7 years ago.
I'm unable to correctly parse a date.....
Here is the code :
String UpdateDateFormat = "Fri Mar 06 00:00:00 CET 2015";
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try{
Date UpdateDateFormated = sdf2.parse(UpdateDateFormat);
}
catch(ParseException ex){
System.out.println(ex.getMessage());
}
I get an exception, "Unparseable date..."
I saw this post : How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012? but it's not working for me.
Any idea ?
Thanks for your help
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Just needed to add the Locale.US.

Convert from String to Date throws Unparseable date exception [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 9 years ago.
I want to convert a Date to a String but I have some problems. My code is this:
SimpleDateFormat formato = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss z yyyy");
String hacer = "Fri Nov 01 10:30:02 PDT 2013";
Date test = null;
test = formato.parse( hacer);
System.out.println("prueba===>" + test);
But nothing something is wrong eclipse shows me this error:
Unparseable date: "Fri Nov 01 10:30:02 PDT 2013"
at java.text.DateFormat.parse(Unknown Source)
some help?
Probably your default locale doesn't support English months in MMM. For example in Poland MMM supports "styczeń" but not "Jan" or "January"
To change this In SimpleDateFormat you need to set locale which supports months written in English, for example
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

Getting the Date format in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I am have this String from a Date object: Mon Mar 25 00:00:00 IST 2013
What is the String representation of the date format?
The code below should work fine.
public static void main(String[] args) throws Exception {
String target = "Mon Mar 25 00:00:00 IST 2013";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
Read the javadoc for the valid patterns.
String yourDate= "Mon Mar 25 00:00:00 IST 2013" ;
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date jDate = new Date(df.parse(yourDate).getTime());
System.out.println(jDate);

Categories