Getting the Date format in Java [duplicate] - java

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

Related

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

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.

how to parse a formatted string date to date object in java [duplicate]

This question already has answers here:
Parse any date in Java
(6 answers)
Closed 4 years ago.
I was trying to parse the formatted string date, was getting parse error
input date is "Wed Nov 11 14:24:46 IST 2015", need output date as "Wed Nov 11 2015 14:24:46 IST"
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd yyyy kk:mm:ss zzz");
String result2 = df2.format(result);
Date result3 = df.parse(result2);
System.out.println(result2);
System.out.println(result3);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
getting error as java.text.ParseException: Unparseable date: "Wed Nov 11 2015 14:24:46 IST"
I have updated my answer to do the parsing as mentioned in your question/comment. See below the explanation:
"Wed Nov 11 14:24:46 IST 2015"
to the following
"Wed Nov 11 2015 14:24:46 IST"
I setup two SimpleDateFormat objects as follow
SimpleDateFormat sourceFormat, destinationFormat;
//this is to format the string to Date object
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
//this is to format the Date object to desired pattern
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
I then set the timezone as follow
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
I use the sourceFormat object to format the date string to Date object as follow:
Date sourceDate = sourceFormat.parse(target);
//output: Wed Nov 11 08:54:46 GMT 2015
I then use the destination format to format the date object that represents the string as follow:
Date destinationDate = destinationFormat.format(d);
//output: Wed Nov 11 2015 14:24:46 IST
Basically in order to get a legit Date object I have to use the first SimpleDateFormat sourceFormat which contains pattern that maps the date in String. Once a legit Date object is created using the String then I use the second formatter to re-format the Date object. Below is full code that should give output if copy/pasted.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class ParseDate {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat sourceFormat, destinationFormat;
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
Date d = sourceFormat.parse(target);
System.out.println(d.toString());
//output: Wed Nov 11 08:54:46 GMT 2015
System.out.println(destinationFormat.format(d));
//output: Wed Nov 11 2015 14:24:46 IST
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}

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 date pattern [duplicate]

This question already has answers here:
How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)
(5 answers)
Closed 7 years ago.
i have this string output 'Wed Apr 01 09:50:31 CEST 2015' can anyone tell me what pattern is that . i need it in order to use it in another function.
#Test
public void test() throws ParseException {
String input = "Wed Apr 01 09:50:31 CEST 2015";
String format = "DD MM hh:mm:ss YYYY"; // i have a wrong format
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println(week);
}
any help to figure out the correct format of date pattern in my case.
Solution to your problem is to override default Date locale using
SimpleDateFormat(String pattern, Locale locale) constructor:
DateFormat dateFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
dateFormat.parse("Tue Jul 13 00:00:00 CEST 2011");
System.out.println(dateFormat.format(new Date()));
Solution copy of How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)

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.

Categories