Here is my code snippet. My local timezone is "Asia/Mumbai".
SimpleDateFormat isoFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date date1 = isoFormat.parse("03/01/2016 09:01 AM");
isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Mumbai"));
isoFormat.applyPattern("dd MMM yyyy HH:mm:ss z");
System.out.println("Current Date and Time in IST time zone: " + isoFormat.format(date1));
But in the output I am getting:
Current Date and Time in IST time zone: 01 Mar 2016 03:31:00 GMT
Can anyone tell why overriding a timezone which is also my local timezone can change it to a GMT?
While the same code works perfectly ok for JST or SGT.
isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Singapore"));
isoFormat.applyPattern("dd MMM yyyy HH:mm:ss z");
System.out.println("Current Date and Time in SGT time zone: " + isoFormat.format(date1));
isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
isoFormat.applyPattern("dd MMM yyyy HH:mm:ss z");
System.out.println("Current Date and Time in JST time zone: " + isoFormat.format(date1));
Output:
Current Date and Time in SGT time zone: 01 Mar 2016 11:31:00 SGT
Current Date and Time in JST time zone: 01 Mar 2016 12:31:00 JST
Check the ID's available for the TimeZone: http://www.mkyong.com/java/java-display-list-of-timezone-with-gmt/
Try with below code:
isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
Related
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")));
I am trying with two sets of date with date format :
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
It works fine for the Date : Fri, 26 Aug 2016 13:55:34 +0000
Not for the Date : Tue, 06 Sep 2016 11:57:14 +0100
Throws exception for the +0100 date.
Unparseable date: "Tue, 06 Sep 2016 11:57:14 +0100" (at offset 0)
at java.text.DateFormat.parse(DateFormat.java:555)
It fails at offset 0, which means that the problem is not related to the timezone but to the day in letters.
You should set the Locale of your SimpleDateFormat.
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
Date d1 = format.parse("Fri, 26 Aug 2016 13:55:34 +0000");
Date d2 = format.parse("Tue, 06 Sep 2016 11:57:14 +0100");
Works without any problem.
If you also need to retrieve the timezone, you will also have to add z to your pattern:
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
You need
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Note the z for the time zone.
The parser ignores the zero (+0000) case if z is not supplied, but not a non-zero (+0100) case. The lenient property controls this behaviour (Acknowledge #Marko Topolnik).
Since you're using English week names, you ought to use the two-argument constructor to SimpleDateFormat, passing Locale.ENGLISH as the second parameter.
I am trying to parse a string of format
Thu Apr 07 11:45:28 AEST 2016
into date object. My code looks like following:
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
try{
Date time = parserSDF.parse("Sat Feb 01 15:00:19 AEDT 2014");
}catch(Exception e){
e.printStackTrace();
}
But I am getting a 'parse error'. I cannot change the input format of the date and I also cannot set my timezone to a static value as this code is to be run on andorid device. How can I parse this string into date ?
Using the java.time framework (JSR 310), you can do:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy");
ZonedDateTime zdt = ZonedDateTime.parse("Sat Feb 01 15:00:19 AEDT 2014", dtf);
System.out.println(zdt);
…which prints:
2014-02-01T15:00:19+11:00[Australia/Sydney]
Though why it picks Sydney instead of Melbourne I am not sure.
I have an array of Strings with the dates e.g.:
Tue, 09 Feb 2016 14:07:00 GMT;
Tue, 09 Feb 2016 19:55:00 GMT.
Now I want to find the most recent date on this list. In order to do that, I try to deserialize these strings to java.util.Date objects and after that compare them.
The code sample of java.util.Date object generation:
strDate = "Tue, 09 Feb 2016 14:07:00 GMT";
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date date;
try {
date = format.parse(strDate);
//Result: Tue Feb 09 16:07:00 IST 2016
System.out.println("Result: " + date.toString());
} catch(ParseException e) {
e.printStackTrace();
}
My questions:
Why is the result in IST 2016 time zone and not in GMT? What does the IST 2016 stand for? Is it India Standard Time or Irish Standard Time or Israel Standard Time?
The initial string is in EEE, dd MMM format, the SimpleDateFormat pattern is also in this format, thus, why the result is in EEE, MMM dd format?
How can get a java.util.Date object in the same timezone as the initial string, in my case — GMT?
Is the approach I'm using to find the most recent date in the list is OK or there is more convenient/modern way to do that in Java 8, e.g., with the usage of LocalDateTime?
You are relying to Date.toString() to print your date when you should format it to a String with a formatter. What you are seeing is just the default pattern of Date.toString(). What you must keep in mind is that a Date does not have a timezone. You are seeing the output with the IST timezone, this must be because the current locale for the JVM is set to some specific locale for which the timezone name is "IST".
With regard to your point 4, yes, you can do it much cleaner with Java Time API introduced in Java 8. You can create a List of your strings to parse, create a DateTimeFormatter to parse it, and keep the maximum date value.
public static void main(String[] args) {
List<String> dates = Arrays.asList("Tue, 09 Feb 2016 14:07:00 GMT", "Tue, 09 Feb 2016 19:55:00 GMT");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime maxDate = dates.stream()
.map(s -> ZonedDateTime.parse(s, formatter))
.max(ZonedDateTime::compareTo)
.get(); // or .orElse(null)
System.out.println(maxDate);
}
This code is using a ZonedDateTime to keep the time-zone of the incoming strings.
Your computer seems to be set to IST. To force GMT output, import java.util.TimeZone and do this in your try block:
format.setTimeZone(TimeZone.getTimeZone("GMT"));
date = format.parse(strDate);
System.out.println("Result: " + format.format(date));
I am trying to convert a date in this format: "Fri Mar 1, 2013 4:30 PM" to a
timestamp value in this format: yyyy-mm-dd hh:mm:ss.
example:
String str = 'Fri Mar 1, 2013 4:30 PM' should output: "2013-01-14 23:59:59"
Here is what I've tried:
String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(date);
This outputs: Sat Jan 02 00:00:00 GMT+05:30 2010
Thanks in Advance
String str = "Fri Mar 1, 2013 4:30 PM";
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd, yyyy hh:mm a");
Date date = sdf1.parse(str);
System.out.println("Date Object:" + date);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
System.out.println("Formatted Date:" + sdf2.format(date));
Output:
Date Object: Fri Mar 01 16:30:00 EST 2013
Formatted Date: 2013-03-01 16:30 PM
Assuming you are getting date and time as Date. So, You need format method of SimpleDateFormatter. Explore more patterns in the API.
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
Date currDate = new Date();
System.out.println("Current Date: " + currDate);
System.out.println("Formatted Date: " + sdf.format(currDate));
}
Output:
Current Date: Wed Feb 06 13:15:19 IST 2013
Formatted Date: 2013-02-06 13:15:750
If you have date in string format, you need to parse it in Date first and then format it.
Example:
String str = "Fri Mar 1, 2013 4:30 PM";
SimpleDateFormat sdf2 = new SimpleDateFormat("E MMM dd, yyyy HH:mm a");
Date parsedDate = sdf2.parse(str);
System.out.println("Parsed Date: " + parsedDate);
sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS a");
System.out.println("Formatted Date: " + sdf2.format(parsedDate));
Output:
Parsed Date: Fri Mar 01 04:30:00 IST 2013
Formatted Date: 2013-03-01 04:30:00 AM
The two previous answers were good answers in 2013. Time is moving on, and so is the handling of time information in Java. If you can use Java 8, do yourself the favour of using the date and time classes in the new java.time package (also backported to Java 6 and 7 in the ThreeTen Backport):
String str = "Fri Mar 1, 2013 4:30 PM";
LocalDateTime ldt = LocalDateTime.parse(str,
DateTimeFormatter.ofPattern("EEE MMM d, uuuu h:mm a", Locale.ENGLISH));
This produces a time without time zone of 2013-03-01T16:30. To format it into the desired output format:
System.out.println(ldt.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")));
This prints:
2013-03-01 16:30:00
Should your want your timestamp in UTC, you may convert from your computer’s time zone like this:
ldt.atZone(ZoneId.systemDefault()).toInstant()
Since I am in the Central European Time zone, on my computer it produces a point in time of 2013-03-01T15:30:00Z (where Z signifies UTC; Instant objects are always printed in UTC).
Link: ThreeTen Backport Home