How to format a string that looks like this
Sat Dec 08 00:00:00 JST 2012
into yyyy-mm-dd i.e.
2012-12-08
From browsing the web, I found this piece of code:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "Sat Dec 08 00:00:00 JST 2012";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
However, I am unable to modify it to accept the first line (Sat Dec 08 00:00:00 JST 2012) as a string and format that into the yyyy-mm-dd format.
What should I do about this? Should I be attempting to modify this? Or try another approach altogether?
Update: I'm using this from your answers (getting error: Unparseable date: "Sat Dec 08 00:00:00 JST 2012")
public static void main(String[] args) throws ParseException{
SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE);
SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE);
Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012");
String destDateString = destFormatter.format(date);
/* String dateInString = "Sat Dec 08 00:00:00 JST 2012";*/
System.out.println(destDateString);
/*try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}*/
}
}
Try this -
SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE);
SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE);
Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012");
String destDateString = destFormatter.format(date);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String dateInString = "Wed Oct 16 00:00:00 CEST 2013";
try {
SimpleDateFormat parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = parse.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Change your formation to this new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Thanks..
You need two SimpleDateFormat objects. One to parse the date from the string using parse() method and the second one to output it in desired format using format() method. For more info about date formatting check the docs.
Related
I am trying to convert the following json string: "Mon Apr 04 00:00:00 CEST 2016" to a new date object by a simpleDateFormat. But i dont see why it wont work hope some one can help me.
String date = "Mon Apr 04 00:00:00 CEST 2016";
I get the following error:
(java.text.ParseException) java.text.ParseException: Unparseable
date: "Mon Apr 04 00:00:00 CEST 2016"
public Date parseDate(String date)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date returnDate = formatter.parse(date);
return returnDate;
}
catch (ParseException e)
{
e.printStackTrace();
return null;
}
}
you need to parse with the locale:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
"EEE MMM ddHH:mm:ss z yyyy"
looks you forgot to put the space after dd:
"EEE MMM dd HH:mm:ss z yyyy"
I'm getting this error:
java.text.ParseException: Unparseable date: "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)"
I have used this SimpleDateFormat can any one suggest me a correct one?
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");
If you are feeding date as "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)". Then it's wrong. Please remove GMT. All time zones are calculated from GMT only.
Try passing date as "Fri Apr 08 2016 00:00:00 +0530 (IST)". It will work.
The correct parse-able date string should be:
Fri Apr 08 2016 00:00:00 IST (+0530)
This little snippet should clear the confusion. It's the reverse of what you're doing:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");
String strDate = format.format(new Date());
System.out.println(strDate);
Output is: Fri Apr 08 2016 17:26:34 IST (+0530)
You can try the pattern EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)
1) Using Java 1.6 :
System.out.println(fromStringToDate("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)", "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)"));
Output (in my system) : Fri Apr 08 00:00:00 IST 2016
Refer this link for timezone values Java TimeZone List
public static Date fromStringToDate(String myPotentialDate,String pattern) throws Exception{
// DateFormat myDateFormat = new SimpleDateFormat(pattern);
String countryCode = "US";
String languageCode = "en";
String timeZone = "Asia/Kolkata";
DateFormat myDateFormat = getDateFormat(pattern,countryCode,languageCode,timeZone);
// We set the Leniant to false
myDateFormat.setLenient(false);
try {
return myDateFormat.parse(myPotentialDate);
}
catch (ParseException e) {
// Unparsable date
throw new Exception("Unparsable date '"+myPotentialDate+"' with pattern '"+pattern+"'. Due to '"+e+"'",e);
}
}
private static DateFormat getDateFormat(String pattern,String countryCode,String languageCode,String timeZoneId){
// We build the Local
Locale myLocale = new Locale(languageCode,countryCode);
// We build the DateFormat with the Local and the pattern
DateFormat myDateFormat = new SimpleDateFormat(pattern,myLocale);
// We set the TimeZone to the correct one
myDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneId));
// We set the Leniant to false
myDateFormat.setLenient(false);
return myDateFormat;
}
2)Using Java 1.8 Java8 Date time API
String countryCode = "US";
String languageCode = "en";
String timeZoneId = "Asia/Kolkata";
LocalDateTime dt = LocalDateTime.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)",
DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)").withLocale(new Locale(languageCode,countryCode)));
ZoneId zoneId= ZoneId.of(timeZoneId);
ZonedDateTime zdt= ZonedDateTime.of(dt, zoneId);
System.out.println(zdt);
Output:
2016-04-08T00:00+05:30[Asia/Kolkata]
Consider the snippet:
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
gives me the output as
01.02.2015 // Ist February 2015
I wish to prevent this to make the user aware on the UI that is an invalid date?
Any suggestions?
The option setLenient() of your SimpleDateFormat is what you are looking for.
After you set isLenient to false, it will only accept correctly formatted dates anymore, and throw a ParseException in other cases.
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
try {
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
} catch (ParseException e) {
// Your date is invalid
}
You can use DateFormat.setLenient(boolean) to (from the Javadoc) with strict parsing, inputs must match this object's format.
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
ddMMyyyy.setLenient(false);
Set the date formatter not to be lenient...
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have a
String dateString = "Fri Feb 14 00:00:00 IST 2014";
I need output in Date datatype like 2014-02-14.
Here is the code which is throwing Parse exception.
Need help in this.
public static void main(String args[]){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date convertedDate = null;
try {
convertedDate = df.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
}
Base point is - Input string should match with date pattern
Raised parse exception as becasue wrong pattern, use this date pattern - EEE MMM dd hh:mm:ss z yyyy
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
You have to convert dateString to matching Date format and then you can format that Date what ever the format you want.
Try this
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat df2 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date date=df2.parse(dateString); // convert stringDate to matching Date format
System.out.println(df1.format(date));
Out put:
2014-02-14 12:00:00
Try this:
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date convertedDate = null;
try {
convertedDate = df.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
}
This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 9 years ago.
I am facing the problem while converting the date:
Current format is:Thu Sep 05 12:07:46 IST 2013(dow mon dd hh:mm:ss zzz yyyy)
I need to convert in to:09/04/2013 11:38 PM PDT(mm/dd/yyyy hh:mm a zzz)
But i am not able to convert.
Try using SimpleDateFormatter. You have to tell it the input/output format, you can do that based on this description (you can also find a few common examples there).
The code will be something like this:
try {
String input = "Thu Sep 05 12:07:46 IST 2013";
DateFormat formatter = new SimpleDateFormat("I leave this to you :-)))");
System.out.println(formatter.parse(input));
} catch (ParseException e) {
e.printStackTrace();
}
Hope that helps.
You can do this
TimeZone tz = TimeZone.getTimeZone("PST8PDT"); // example
// required format. Remember M is for month, m for miniute
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a zzz");
df.setTimeZone(tz);
String text = df.format(new Date());// current time
System.out.println(text);
Also please check this TimeZones in Java
You try to convert dateformat and timeZone as well, so you need to convert the timezone in your code.
SimpleDateFormat sf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
isoFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = isoFormat.parse("mm/dd/yyyy hh:mm a zzz");
this may help you.
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
OutPut
09/05/2013 00:07:46 AM IST
update
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
dfto.setTimeZone(zone);
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
output
09/04/2013 11:37:46 AM PDT