I'm parsing a UTC Date string with SimpleDateFormat and it is parsed as the previous date.
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
This string "Fri, 06 Apr 2012 04:00:00 GMT" is being parsed as 4/5/2012. Why? Thanks.
Below is example of date conversion...
For your program, do changes accordingly and let me know what output you are getting.
String dateStr = "Thu Jan 19 2012 01:00 PM";
DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse( dateStr );
} catch ( ParseException e ) {
e.printStackTrace();
}
String formattedDate = "";
if( date != null ) {
formattedDate = writeFormat.format( date );
}
System.out.println(formattedDate);
Output is 2012-01-19 13:00:00
Do the changes as per output you are expecting.
Beware of JDK 1.5.0_22 (linux). Produces incorrect date. Switched to JDK 1.6 problem gone.
Related
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);
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);
I am trying to convert String to Date to compare from current Date and it throws parse Exception
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
try {
java.util.Date cookiedate = format.parse("Tue Apr 29 11:40:55 GMT+04:00 2014");
Calendar currentDate = Calendar.getInstance();
String dateNow = format.format(currentDate.getTime());
java.util.Date currDate = format.parse(dateNow);
if (currDate.getTime() > cookiedate.getTime()) {
return true;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In your format, "yyyy-MM-dd HH:mm:ss" will match date string like "2013-03-30 15:57:00", so you get a ParseException.
If you want to parse "Tue Apr 29 11:40:55 GMT+04:00 2014", you should use "EEE MMM dd HH:mm:ss z yyyy" Change your code to
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
At first, you should parse the string date to Date object using EEE MMM dd HH:mm:ss Z yyyy format then convert that Date to yyyy-MM-dd HH:mm:ss format as follows...
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try {
Date cookiedate = format.parse("Tue Apr 29 11:40:55 GMT+04:00 2014");
Calendar currentDate = Calendar.getInstance();
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
cookiedate = format.parse(format.parse(cookiedate));
String dateNow = format.format(currentDate.getTime());
Date currDate = format.parse(dateNow);
if (currDate.getTime() > cookiedate.getTime()) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
I think it is pretty obvious that formatted date "Tue Apr 29 11:40:55 GMT+04:00 2014" does not match format "yyyy-MM-dd HH:mm:ss".
If you are using format "yyyy-MM-dd HH:mm:ss" you should parse strings like 2014-03-30 10"59:23.
If you want to parse string like "Tue Apr 29 11:40:55 GMT+04:00 2014" you should use format like EEE MMM dd HH:mm:ss z yyyy. (I am not sure about z, probably it should be Z).
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
I am using SimpleDateFormat and I am getting ParseException as shown below.
java.text.ParseException: Unparseable date: "Mon Jul 02 21:56:10 AST 2012"
Code I have have is
String dateStr = "Mon Jul 02 21:56:10 AST 2012";
DateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy ");
DateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse(dateStr);
} catch (ParseException e) {
System.out.println("Error in parsing date ********");
}
String formattedDate = "";
if (date != null) {
formattedDate = writeFormat.format(date);
}
System.out.println("Formatted date is " + formattedDate);
Any idea where I am going wrong?
Update 1
I also tried with
DateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy ");
^
but still same exception.
Your code works (with z, and not Z), as soon as I specify that the date format should use the symbols of the English locale:
SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
readFormat.setDateFormatSymbols(DateFormatSymbols.getInstance(Locale.ENGLISH));
As per eran, you also have extra space after yyyy: yyyy "). Remove that extra space.
The format code Z is for timezone offset, like -0800, while the format code z is for the written format, such as PST or CST, according to what's described at SimpleDateFormat. Double-check that your parse pattern has the intended capitalization.