Reputation: 3801
In java I need to make a Calendar object from a String in the format:
yyyy-MM-dd'T'HH:mm:ss
This string will always be set as GMT time. So here's my code:
public static Calendar dateDecode(String dateString) throws ParseException
{
TimeZone t = TimeZone.getTimeZone("GMT");
Calendar cal = Calendar.getInstance(t);
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date d = date.parse(dateString);
cal.setTime(d);
return cal;
}
And then:
Calendar cal = Calendar.getInstance();
try
{
cal = dateDecode("2002-05-30T09:30:10");
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
int month = cal.get(Calendar.MONTH)+1;
And I get the following output:
Timezone: GMT+00:00 date: 2002-5-30 time: 7:30:10
Which is as you can see wrong since the time provided is in GMT and not CET. I think what happens is that it think the time provided is in CET (which is my current timezone) and therefore converts the time from CET to GMT and therefore deducts two hours from the final result.
Could anyone help me with this?
Thanks
Btw: I do not wish to use JodaTime for different reasons.
Upvotes: 2
Views: 4209
Reputation: 340230
Your string format happens to comply with the ISO 8601 standard.
If you are certain the string of the date-time value is meant for UTC rather than some other offset-from-UTC or time zone, it should carry a Z
at the end. The Z
is short for Zulu
and means UTC.
String input = "2002-05-30T09:30:10" + "Z" ;
The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.parse( "2002-05-30T09:30:10" + "Z" );
instant.toString(): 2002-05-30T09:30:10Z
ZonedDateTime
If you want to see that same moment as the wall-clock time of a particular region, apply a ZoneId
to get a ZonedDateTime
object.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as CET
or EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdt = instant.atZone( z );
zdt.toString(): 2002-05-30T11:30:10+02:00[Europe/Paris]
See this code run live at IdeOne.com.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 1
Reputation: 36423
Here is some code that could help you out with setting the timzones before parsing them:
// sdf contains a Calendar object with the default timezone.
Date date = new Date();
String formatPattern = ....;
SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
TimeZone T1;
TimeZone T2;
....
....
// set the Calendar of sdf to timezone T1
sdf.setTimeZone(T1);
System.out.println(sdf.format(date));
// set the Calendar of sdf to timezone T2
sdf.setTimeZone(T2);
System.out.println(sdf.format(date));
// Use the 'calOfT2' instance-methods to get specific info
// about the time-of-day for date 'date' in timezone T2.
Calendar calOfT2 = sdf.getCalendar();
another similar question I found might help too: How to set default time zone in Java and control the way date are stored on DB?
EDIT:
Here is a great tutorial on Java & Dates too: http://www.tutorialspoint.com/java/java_date_time.htm
Upvotes: 4