java date format - GMT 0700 (PDT) - java

This is the date format that I need to deal with
Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)
But I don't get what the last two parts are. Is the GMT-0700 fixed? Should it be something like this?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT-0700' (z)");

No, it is not fixed. It is a TimeZone. You can match it with Z in the date format.
To be more precise, in SimpleDateFormat formats :
Z matches the -0700 part.
GMT is fixed. Escape it with some quotes.
z matches the PDT part. (PDT = Pacific Daylight Time).
The parenthesis around PDT are fixed. Escape them with parenthesis.
You can parse your date with the following format :
EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'
Another remark : Wed Aug contains the day and month in English so you must use an english locale with your SimpleDateFormat or the translation will fail.
new SimpleDateFormat("*format*", Locale.ENGLISH);

Here is the Javadoc:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
For this example: Wed Aug 21 2013 00:00:00 GMT-0700 (PDT), you'd want this format:
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaDate {
public static void main (String[] args) throws Exception {
String s= "Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)";
SimpleDateFormat sdf =
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'z '('Z')'");
Date d = sdf.parse (s);
System.out.println ("Date=" + d + "...");
}
}
EXAMPLE OUTPUT: Date=Tue Aug 20 23:00:00 PDT 2013...
Thanx to Arnaud Denoyelle above for his edits!

Related

How to get Date in the below Format "dd MMM, yyyy z"

How to make the date to have a GMT offset like mentioned here
import java.util.*;
import java.text.*;
import java.lang.*;
class TFTest
{
public static void main(String[] args)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM, yyyy z");
Date dt = new Date();
System.out.println("\n\n\nparsed date:"+sdf.format(dt)+"\n\n");
}
}
the above program outputs the value as
parsed date:02 Aug, 2016 IST.
But I want the value to be parsed date:02 Aug, 2016 GMT +05:30
How to get in the specified format ..?
The pattern that should work is dd MMM, yyyy 'GMT' XXX indeed X is the timezone in ISO 8601 which seems to be what you are looking for.
Output:
parsed date:02 Aug, 2016 GMT +05:30
Try, for more documentation visit simpledateformat
"dd MMM, yyyy 'GTM' XXX"
This pattern "dd MMM, yyyy z ZZZZ" will print in the given format
Format formatter = new SimpleDateFormat("dd MMM, yyyy z ZZZZ");
Result like :
02 Aug, 2016 GMT +0000

SimpleDate Format parsing error

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]

Getting incorrect parsed date when using SimpleDateTimeFormatter with time zone

I've the following code:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String s2 = "Mon Oct 19 19:52:21 IST 2015";
System.out.println(format.parse(s2));
I expected that the code would print "Mon Oct 19 07:22:21 PDT 2015", but to my surprise it printed "Mon Oct 19 10:52:21 PDT 2015". I can see that "IST" is a valid time zone for "z". However, the following code worked fine:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String s2 = "Mon Oct 19 19:52:21 GMT+05:30 2015";
System.out.println(format.parse(s2));
Please help.
Using IST is dangerous as it could stand for Indian or Israel ST.
Assuming you meant Indian ST, try this:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
format.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
String s2 = "Mon Oct 19 19:52:21 IST 2015";
System.out.println(format.parse(s2));
Also, follow suggestion from #Meno's answer
The code expression
System.out.println(format.parse(s2));
always prints your instant/moment (an object of type java.util.Date) in your system timezone which is apparently PDT. You implicitly apply the method toString()on java.util.Date. Use a dedicated formatter like SimpleDateFormat and set the timezone on this formatter to achieve the desired result.
Finally you need two formatters, one for parsing and one for printing.

wrong date in SimpleDateTime parse

This is the string that I have:
Sat, Nov 02, 2013 at 5:10 pm
I'm trying to parse it into a date time using this formatter:
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, YYYY 'at' K:mm a");
However, this is what it returns when I use it to parse the date string:
Sat Jan 05 17:10:00 CST 2013
I assume I'm getting the formatter wrong, but I can't figure out where.
Capital YYYY is the format for something called the "week year". You want the lowercase yyyy for the actual year.
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, yyyy 'at' K:mm a");
With this change I output the parsed date and get:
Sat Nov 02 17:10:00 PDT 2013
(I'm in the Pacific time zone.)

Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]

This question already has answers here:
How to convert date in to yyyy-MM-dd Format?
(6 answers)
How can I convert Date.toString back to Date?
(5 answers)
Java - Unparseable date
(3 answers)
Closed 5 years ago.
I got problem with date parse example date:
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());
parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
got exception
Exacly I want parse this format date to yyyy-MM-dd
I try:
SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
take :
java.text.ParseException: Unparseable date: "Wed Oct 16 00:00:00 CEST 2013"
OK I change to and works :
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
I'm going to assume that Locale.getDefault() for you is pl-PL since you seem to be in Poland.
English words in date strings therefore cause an unparseable date.
An appropriate Polish date String would be something like
"Wt paź 16 00:00:00 -0500 2013"
Otherwise, change your Locale to Locale.ENGLISH so that the SimpleDateFormat object can parse String dates with English words.
Instead of using Locale.default that you and others often don't know which default, you can decide by using locale.ENGLISH because I see your string date is format in English. If you are at other countries, the format will be different.
Here is my example code:
public static void main(String[] args) {
try {
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
System.out.println("date: " + date.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
}
The result will be : date: Wed Oct 16 05:00:00 ICT 2013. Or you can decide which part of this date to be printed, by using its fields.
Hope this help :)
I think the original Exception is due to Z in your format.
Per documentation:
Z Time zone RFC 822 time zone -0800
most likely you meant to use lower case z

Categories

Resources