Change GMT to EST time in Java 6 - java

I am trying to convert a GMT time zone from other server to EST considering day light saving to display the correct date but not able to do.
The GMT time format is coming in json String as "yyyy-MM-dd 'T' HH:mm:ss 'Z'". To this format we are setting time zone as EST but not getting correct result.
Example - date coming as "2020-06-02T03:53:57Z" , while the correct date in EST when created was "2020-06-01T11:53:57Z".

To convert between two explicit time zones, specify those time zones on the SimpleDateFormat objects used to parse and re-format the date string.
String input = "2020-06-02T03:53:57Z";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = inputFormat.parse(input);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
outputFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(outputFormat.format(date));
Output
2020-06-01 23:53:57 EDT
As you can see, in June, the time zone is EDT, not EST, and it is certainly not Z, aka UTC.

Related

How to convert Date String to joda DateTime?

I have a date string "Wed Nov 20 00:00:00 IST 2019". How do I convert it to joda DateTime with the pattern "yyyyMMdd".
dateObject.setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd"));
The problem with your String pattern is, that JodaTime does not recognize the 'IST' timezone. (See http://joda-time.sourceforge.net/timezones.html for a list of supported time zones.)
If you always want to parse the date in the same time zone, you could use:
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss 'IST' yyyy").withZone(DateTimeZone.forID("Indian/Mahe"));
DateTime parsed = DateTime.parse("Wed Nov 20 00:00:00 IST 2019", dateTimeFormatter);
Note that I have used IST as a string literal in the pattern format, i.e., this will only work if your date strings always includes the "IST" string.
To add fixed time zone information to your parsed date use withZone on the formatter. I picked a random Indian timezone known to JodaTime, "Indian/Mahe" in this case. Look up the one that matches your time zone in the list of supported time zones.

Convert GMT to BST Java

I have a date format yyyy/mm/dd hh:mm:ss in GMT format. I want to convert the date from GMT format to BST time. What's the simplest way to do this?
With Java 8, the easiest way is probably to:
parse the date into a LocalDateTime
use it to create a ZonedDateTime using GMT time zone
change the time zone to BST
get the LocalDateTime of that new date
Sample example:
String date = "2015/03/09 10:32:00";
LocalDateTime gmt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
ZonedDateTime instant = ZonedDateTime.of(gmt, ZoneId.of("GMT"));
LocalDateTime bst = instant.withZoneSameInstant(ZoneId.of("Europe/London")).toLocalDateTime();
System.out.println(bst);
If you change the month to July, for example, you should see an offset of one hour as expected.
(I assumed that BST is British Summer Time - it could also be Bangladesh Standard Time: in general it is better to use the full name of the time zone to avoid ambiguities).

TimeZone Conversion with SimpleDateFormat in Java

I have a SimpleDateFormat parser that parse in this way:
sdf = new java.text.SimpleDateFormat("yyyy-MM-DD HH:mm:ss z").parse("2013-10-25 17:35:14 EDT");
log.debug(sdf);
This give me Sat Jan 26 03:05:14 IST 2013
What am i missing here?
First of all, DD stands for day in year, You should use dd instead.
Also, if you want to print a date in a specific format, you need to use two SimpleDateFormats.
SimpleDateFormat.parse returns a Date object represents the date you specified at the given format.
The Date object itself is saved as a regular Date, no format attached to it.
If you want to print it in a specific format, you need to use another SimpleDateFormat and call format method.
you should use Format
SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:SS z");
String sdf = sdf1.format(sdf1.parse("2013-10-25 17:35:14 EDT"));
There are two things.
sdf is an object of Date, which represents a specific instant in time (milliseconds elapsed since another instant known as "the epoch"). There is no format which is known to this object. And how this object is printed is solely handled by its class' toString method, which prints the date in this format:
dow mon dd hh:mm:ss zzz yyyy
This is exactly what you see in your output. Note that the timezone of the machine running the program is printed in this case. If you wish to print this object in a format of your choice you should use a DateFormat. To get output for a specific timezone you have to explicitly tell it to the DateFormat object (see below).
Another thing is you should be using dd instead of DD in the pattern. D is for day in year and d is for day in month, which I believe is what you want. Now keeping in mind both the points, this is one of the solutions to your problem:
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("EDT")); // Time Zone for output
Date d = sdf.parse("2013-10-25 17:35:14 EDT");
System.out.println(sdf.format(d)); // You need format method
which outputs:
2013-10-25 17:35:14 EDT
What are the people answering not getting here is more the question:
2013-10-25 17:35:14 EDT != Sat Jan 26 03:05:14 IST 2013
I think it is because 'EDT' is the timezone and so, when it is 17:35 in EDT is is 3:05 in the UK, ignoring Daylight saving adjustments.

Getting date in GMT from unix timestamp [duplicate]

This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 9 years ago.
I have written the following code to get the date in GMT from a unix timestamp
private Date converToDate(String unixTimeStamp)
{
//unix timestamps have GMT time zone.
DateFormat gmtFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
//date obtained here is in IST on my system which needs to be converted into GMT.
Date time = new Date(Long.valueOf(unixTimeStamp) * 1000);
String result = gmtFormat.format(time);
return lineToDate(result, true);
}
this code upon execution has
Mon May 27 02:57:32 IST 2013
value in the date variable and
Sun May 26 21:27:32 GMT 2013
in the result variable , How do I directly get the value in result variable into date variable ?
This is the problem, conceptually:
//date obtained here is in IST on my system which needs to be converted into GMT.
Date time = new Date(Long.valueOf(unixTimeStamp) * 1000);
A Date doesn't have a time zone. This is the value you want. The fact that when you call toString() it converts it to your local time zone is irrelevant to the value that it's actually representing. A Date is just a number of milliseconds since the Unix epoch (1st January 1970, midnight UTC). So your whole method can be:
private static Date convertToDate(String unixTimeStamp)
{
return new Date(Long.valueOf(unixTimeStamp) * 1000);
}
You don't need any kind of formatter, as you're not really trying to get a textual representation.
I would advise you to use Joda Time for date/time work if you can, by the way - it's a much cleaner API.
A Date is just the wrapper for a long, which contains a number of milliseconds.
What you're seeing is the default toString() representation of the Date object, which uses your default timezone (IST) to transform the date into a readable string. If you want the date represented as a string using the GMT timezone, just do what you did: use a date format with the GMT time zone.
The Date object represents an instant on the universal timeline, and doesn't have any timezone.

TimeZone gost haunting my datetime parsing

I cannot use Joda. When I try to create a Date or Calendar from a String representing a date or datetime in UTC, the resulting object has a timezone associated with it...
Here's the code:
Case 1:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyDDD");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse("2012018");
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
cal.setTime(date);
System.out.println(cal.getTime()); //Tue Jan 17 19:00:00 EST 2012 !?!*&#&??
Case 2:
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse("1202011431");
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
cal.setTime(date);
System.out.println(cal.getTime()); //Wed Feb 01 09:31:00 EST 2012 !?!*&#&??
Objects of type java.util.Date are an absolute amount of time since Epoch. When you create a Date object from a Calendar, it loses any TimeZone that the Calendar may have been using and just gives you a raw quantity of time. EST isn't really 'associated' with your Date, it's just that the toString method on Date uses the JVM default timezone to create a human readable representation.
It's important to understand that the usage of Calendar in the above code samples is doing absolutely nothing. Changing just the timezone like that changes only the human readable representation of that Calendar. The raw number of milliseconds since epoch is the same before and after you call setTime and getTime.
(All of the methods on java.util.Date that are related to human readable representations are deprecated and do not work properly, it is best to simply pretend they do not exist.)
When you print the time, it is always printed in local timezone configured on your machine and that is what you want (Store and represent date/time in UTC and convert to user specific timezone during presentation).
Yes, dates in Java have a time zone associated with them. The toString on a Date prints the date in the local timezone. If you do not want that, use a DateFormat to not show it. Note, that you will need to set the timezone on the DateFormat to UTC to get what you want.

Categories

Resources