How can I get the time zone from a datetime string with this format 2013-08-15T13:00:00-07:00?
You could take the time offset from Andreas answer and use below sinppet to get TimeZone object.
TimeZone tmzo = TimeZone.getTimeZone("GMT"+offset);
System.out.println(tmzo.getID());
if Java 8, you can parse directly with ISO_ZONED_DATE_TIME format
String input = "2013-08-15T13:00:00-07:00";
ZonedDateTime zDateTime = ZonedDateTime.parse(input, DateTimeFormatter.ISO_ZONED_DATE_TIME);
ZoneId zone = zDateTime.getZone();
You can use a regular expression. The following assumes XML dateTime syntax:
String input = "2013-08-15T13:00:00-07:00";
Matcher m = Pattern.compile("(?:[+-]\\d{2}:\\d{2}|Z)$").matcher(input);
if (m.find())
System.out.println("Time zone: " + m.group());
else
System.out.println("No time zone found");
In Java 7 you can parse with and without TZ and than calculate the offset, taking your current TZ offset into account.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date withTZ = sdf.parse("2013-08-15T13:00:00-07:00");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date noTZ = sdf2.parse("2013-08-15T13:00:00-07:00");
2013-08-15T13:00:00-07:00
the last -7:00 means GMT-7
That is MST (Mountain Standard Time)
That timezone is using in US and Canada
Related
I want a DateFormatter in java so that i can specify some special character as well as digits in a date expression. For ex :
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
Here dd is used to specify the day of month which is numeric.
But i have a requirement to create a date as below :
String stringDate = "2017-12-??T00:00Z";
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
formatter.parse(stringDate);
I get an unparseable exception as the DAY specified here is ?? . Is there any workaround for this or shall i have to write a new parser ?
Thanks
Try escaping the additional literals using single quote
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-'??T'HH:mm:ss.SSS zzz");
Also the value and the format given should match(Can edit the string date as required), in your case following syntax will work.
String stringDate = "2017-12-??T00:00Z";
Date date = (new SimpleDateFormat("yyyy-MM-'??T'HH:mmZ")).parse(stringDate.replaceAll("Z$", "+0000"));
System.out.println("date: " + (new SimpleDateFormat("yyyy-MM-dd'??T'HH:mmZ")).format(date));
Please note that 'Z' indicates that the timezone conforms to the RFC 822 time zone standard as well.
Edit: Consider a scheduler. Your comment may sound like what you need is a scheduler, for example Quartz scheduler. I include a link at the bottom. Then convert user input not to a YearMonth, OffsetDateTime or any other date-time object (because they don’t fit), but into a syntax that your scheduler can accept.
Original answer
I am giving you a couple of suggestions. It’s with reservation though: I don’t understand why you want this, not even exactly what you want, so these suggestions may not be the right ones for you.
One suggestion I am pretty sure of, though: do use java.time, the modern java date and time API, for your date and time work. It is so much nicer to work with than the old, poorly designed and long outdated date-time classes that include the notoriously troublesome SimpleDateFormat class.
Parsing year and month: If you just want the year and the month from a string that has question marks instead of the day of month, parse into a YearMonth:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-'??T'HH:mmX");
String stringDate = "2017-12-??T00:00Z";
YearMonth ym = YearMonth.parse(stringDate, formatter);
System.out.println("Year and month are " + ym);
Output from this snippet is:
Year and month are 2017-12
Parsing all information from the string: If you need time of day and offset from the same string too, just parse the string once and get the various information from the parse result:
TemporalAccessor parsed = formatter.parse(stringDate);
YearMonth ym = YearMonth.from(parsed);
System.out.println("Year and month are " + ym);
LocalTime time = LocalTime.from(parsed);
System.out.println("Time of day is " + time);
ZoneOffset offset = ZoneOffset.from(parsed);
System.out.println("UTC offset is " + offset);
Year and month are 2017-12
Time of day is 00:00
UTC offset is Z
Using a default day of month: If you know what day of month you want instead of the question marks, specify it as a default value:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-'??T'HH:mmX")
.parseDefaulting(ChronoField.DAY_OF_MONTH, 23)
.toFormatter();
String stringDate = "2017-12-??T00:00Z";
OffsetDateTime dateTime = OffsetDateTime.parse(stringDate, formatter);
System.out.println("Date and time is " + dateTime);
Date and time is 2017-12-23T00:00Z
Accepting both numbers and question marks: If the date can be given as either numeric or question marks, use optional parts in the format pattern strings. Such are enclosed in square brackets:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-[??][dd]'T'HH:mmX")
.parseDefaulting(ChronoField.DAY_OF_MONTH, 23)
.toFormatter();
String stringDate = "2017-12-??T00:00Z";
OffsetDateTime dateTime = OffsetDateTime.parse(stringDate, formatter);
System.out.println("Date and time is " + dateTime);
stringDate = "2018-02-16T00:00Z";
dateTime = OffsetDateTime.parse(stringDate, formatter);
System.out.println("Date and time is " + dateTime);
Date and time is 2017-12-23T00:00Z
Date and time is 2018-02-16T00:00Z
Tutorial links
Cron Trigger Tutorial from the Quartz Scheduler documentation.
Oracle tutorial: Date Time explaining how to use java.time.
I've issue in format of java for ISO 8601, I'm using this code
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
//yyyy-MM-dd'T'HH:mm:ss.shh:mm
Date date = Calendar.getInstance().getTime();
String cDate = format.format(date);
Log.d("Date","Date is " + cDate);
My result is:
Date is 2019-04-03T04:37:52+0000
if I'm using this format yyyy-MM-dd'T'HH:mm:ssZ. And the result is:
Date is 2019-04-03T04:49:33.3304:49
if I'm using this format yyyy-MM-dd'T'HH:mm:ss.shh:mm. I've been tried my solutions but not is giving me my desired solution.
My desired solution is 2008-09-15T15:53:00+05:00. I try Offset but it's not working for API level 23, It's only for API level 28.
Here is the format mask you should be using:
yyyy-MM-dd'T'HH:mm:ssXXX
From the documentation for SimpleDateFormat, we can see that X represents the ISO 8601 time zone, which shows the timezone in terms of hours shifted from GMT.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
format.setTimeZone(TimeZone.getTimeZone("Africa/Cairo"));
Date date = Calendar.getInstance().getTime();
String cDate = format.format(date);
System.out.println("Date is " + cDate);
Date is 2019-04-03T06:41:30+02:00
Note that I needed to assign a time zone other than GMT, because GMT just returns Z for the time zone.
Tim was right but his given pattern works for java, Not for that java which is in android.
For Android java use this pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
Then out put will be
Date is 2019-04-03T08:29:27+00:00
I have this date in this format:
String date = "2018-12-08T07:50:00+01:00";
And I'd like to get the local time in this format (adding the hours over GMT) but I'm not able to do it
date = "2018-12-08 08:50:00";
Other example:
String date = "2018-12-08T07:50:00+04:00";
Result:
date = "2018-12-08 11:50:00";
Any help?
As Sun already said in another answer, you misunderstood: 2018-12-08T07:50:00+01:00 is the same point in time as 2018-12-08 06:50:00 in UTC (roughly the same as GMT), not 08:50. +01:00 means that the time comes from a place where the clocks are 1 hour ahead of UTC, so to get the UTC time 1 hour should be subtracted, not added.
DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String date = "2018-12-08T07:50:00+01:00";
OffsetDateTime dateTime = OffsetDateTime.parse(date);
OffsetDateTime dateTimeInUtc = dateTime.withOffsetSameInstant(ZoneOffset.UTC);
date = dateTimeInUtc.format(desiredFormatter);
System.out.println(date);
Output from this snippet is:
2018-12-08 06:50:00
Using your other example, 2018-12-08T07:50:00+04:00, the output is
2018-12-08 03:50:00
I am taking advantage of the fact that your string is in ISO 8601 format. OffsetDateTime parses this format as its default, that is, we don’t need to specify the format in a DateTimeFormatter (as we do for your desired result format).
Link: Wikipedia article: ISO 8601
2018-12-08T07:50:00+01:00, the +01:00 in the end does not mean adding hh:mm, it means the datetime is already local date time, in GMT it is 2018-12-08T06:50:00.
You can use string.replaceAll to remove T and +01:00:
String input = "2018-12-08T07:50:00+01:00";
input = input.replaceAll("T", " ");
input = input.replaceAll("\\+.*", "");
System.out.println(input); // 2018-12-08 07:50:00
or parse and re-format it:
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-08T07:50:00+01:00");
String time = offsetDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).replace("T", " ");
System.out.println(time); // 2018-12-08 07:50:00
I want to parse the following string in Java and convert it to a date:
DTSTART;TZID=America/Los_Angeles:20140423T120000
I tried this:
SimpleDateFormat sdf = new SimpleDateFormat("'DTSTART;TZID='Z':'yyyyMMdd'T'hhmmss");
Date start = sdf.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000");
And this:
SimpleDateFormat sdf = new SimpleDateFormat("'DTSTART;TZID='z':'yyyyMMdd'T'hhmmss");
Date start = sdf.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000");
But it still doesn't work. I think the problem is in America/Los_Angeles.
Can you help me please?
Thank you
Try this one using TimeZone.
Note: You have to split your date string before doing this operation.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'hhmmss");
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
sdf.setTimeZone(tz);
Date start = sdf.parse("20140423T120000");
In SimpleDateFormat pattern Z represent RFC 822 4-digit time zone
For more info have a look at SimpleDateFormat#timezone.
If you look for a solution how to parse the whole given string in one and only one step then Java 8 offers this option (the pattern symbol V is not supported in SimpleDateFormat):
// V = timezone-id, HH instead of hh for 24-hour-clock, u for proleptic ISO-year
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("'DTSTART;TZID='VV:uuuuMMdd'T'HHmmss");
ZonedDateTime zdt =
ZonedDateTime.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000", dtf);
Instant instant = zdt.toInstant();
// if you really need the old class java.util.Date
Date jdkDate = Date.from(instant);
I managed to convert a valid date string in a different timezone to UTC as follows.
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter DATETIME_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT);
DateTimeZone dateTimeZone = DateTimeZone.forID("-03:00");
//date is 2000-01-01 00:00:00 -03:00
DateTime date = DATETIME_FORMATTER.withZone(dateTimeZone).parseDateTime("2000-01-01 00:00:00"));
System.out.println("Current date is: " + date.toString());
//now convert to UTC
DateTime convertedDate = date.toDateTime(DateTimeZone.UTC);
System.out.println("Converted date: " + date.toString());
The result is
Current date is: 2000-01-01T00:00:00.000-03:00
Converted date: 2000-01-01T03:00:00.000Z
Is there a shorter/better way of doing this? I want the final date to be a Joda-Time DateTime object.
You can convert the time zone of any DateTime using withZone(). If the input string doesn't specify the time-zone offset then you have to add it as you are doing, so your code is fairly optimal.
One improvement over your example code might be replacing the hard-coded "-3:00" offset with a time zone name. That would allow Joda-Time to make adjustments for any possible Daylight Saving Time (DST). See doc for DateTimeZone.forID().
This:
DateTimeZone dateTimeZone = DateTimeZone.forID("America/Sao_Paulo");
instead of this:
DateTimeZone dateTimeZone = DateTimeZone.forID("-03:00");
Time Zone list (possibly outdated, read note):
http://joda-time.sourceforge.net/timezones.html