How to set the TimeZone for String parsing in Android - java

I try to parse a String and set a time zone, but I can't produce the desired result.
String dtc = "2014-04-02T07:59:02.111Z";
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = null;
try {
date = readDate.parse(dtc);
Log.d("myLog", "date "+date);
} catch (ParseException e) {
Log.d("myLog", "dateExcep " + e);
}
SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm");
writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
String dateString = writeDate.format(date);
At the output of the variable "dateString" still gives the time 07:59:02 , and I want to make it +4 hours in advance that is 11:59:02

You need to instruct the read-formatter to interprete the input as UTC (GMT - remember that Z stands for UTC in ISO-8601-format):
String dtc = "2014-04-02T07:59:02.111Z";
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
readDate.setTimeZone(TimeZone.getTimeZone("GMT")); // missing line
Date date = readDate.parse(dtc);
SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm");
writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
String s = writeDate.format(date);
Then you will get:
02.04.2014, 11.59

Joda-Time
Doing date-time work much easier and simpler with the Joda-Time library than with the notoriously troublesome bundled java.util.Date & .Calendar classes.
Time Zone
Using proper time zone names, rather than a specific offset, is generally a wiser approach.
Example Code
Here is some example code using Joda-Time 2.3.
Notice:
Joda-Time has built-in parsers for strings in ISO 8601 format. No need to instantiate parsing or formatting objects.
Joda-Time is both parsing the UTC string and adjusting it to another time zone all in one call to the DateTime constructor.
When converting to a UTC-based dateTime in the last line, we still have the same moment in the timeline of the Universe (same count of milliseconds since Unix epoch).
Source…
String input = "2014-04-02T07:59:02.111Z";
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Dubai" );
DateTime dateTimeDubai = new DateTime( input, timeZone ); (a) Parse, (b) Adjust time zone.
DateTime dateTimeUtc = dateTimeDubai.withZone( DateTimeZone.UTC );
Dump to console…
System.out.println( "input: " + input );
System.out.println( "dateTimeDubai: " + dateTimeDubai );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
When run…
input: 2014-04-02T07:59:02.111Z
dateTimeDubai: 2014-04-02T11:59:02.111+04:00
dateTimeUtc: 2014-04-02T07:59:02.111Z

you can take whatever pattern you want like i have date+ time zone. so you can set according to you. here is my code.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
here i have set the date format.
String currentDate = dateFormat.format(currentLocalTime);
String localTime = date.format(currentLocalTime);// to get the time zone i have used this.
String newtime=currentDate+localTime;// and then i have apeend this now print log you will get the desire result.

Related

Joda DateTime automatically change timezone when parsing string

I have string of a date in "iso8601" format and when I parse it using Joda "DateTime", the time zone of date changes automatically.
DateTime dateTime = new DateTime( "2017-05-22T08:10:00.000+0300" ) ;
System.out.println(dateTime);
and its output is:
2017-05-22T09:40:00.000+04:30
As you see time zone of first string is +3:00 and the time zone after parsing is +04:30. How can I parse first string without changing time zone? (so the time zone remains +03:00 even after parsing)
This constructor use default timezone of user. You need to set timezone manually with DateTime(Object object, DateTimeZone zone) construnctor.
Or, parse this string with usage of withOffsetParsed() like this:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
DateTime dateTime = formatter.withOffsetParsed().parseDateTime("2017-05-22T08:10:00.000+0300");
You have to setup time zone manually something like this:
String str = "2017-05-22T08:10:00.000+0300" ;
DateTime dateTime = new DateTime() ;
System.out.println(dateTime);
String tzName = str.substring(text.length() - 5);
DateTimeZone tz = DateTimeZoneDateTimeZone forID(str.substring(3) + ":" + str.substring(text.length() - 2))
System.out.println(dateTime.withZone(tz));
You can use the inbuilt functionalities of JDK 8 date-time API to solve this easily. The following code prints 2017-05-22T08:10+03:00.
DateTimeFormatter df = DateTimeFormatter.ISO_OFFSET_DATE_TIME ;
OffsetDateTime date1 = OffsetDateTime.parse("2017-05-22T08:10:00.000+03:00", df);
System.out.println(date1); //prints 2017-05-22T08:10+03:00

issue with date/timezone in Java

I need to display time zone in CET in my java application.
And I am using following code to achieve this.
String OLD_FORMAT = "yyyyMMdd HH:mm:ss";
String NEW_FORMAT = "dd.MM.yyyy HH:mm:ss";
String date = "20140217 14:45:28";
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
TimeZone zone = TimeZone.getTimeZone("GMT+1");
sdf.setTimeZone(zone);
Date d = null;
d = sdf.parse(date);
sdf.applyPattern(NEW_FORMAT);
date = sdf.format(d);
and I am using the date object to print the date on UI.
OR
TimeZone zone = TimeZone.getTimeZone("CET");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
sdf.setTimeZone(zone);
But using the either of above piece of code i am getting GMT time which is one hour behind CET.
FOr example if I execute the code now, I will get 1:32:50 PM where as its 2:32:50 PM as per http://wwp.greenwichmeantime.com/time-zone/europe/european-union/central-european-time/
Any one any idea what might be going wrong here ?
UPDATE : I have found the issue. I made a silly mistake as I had to set the time first to GMT (the datetime i was getting was in GMT) and then change it to CET. Its working now. Thanks much everyone for the reply.
Maybe you are passing the wrong date to the SimpleDateFormat instance. I've written a small to test your code and it seems to work:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class Test {
public static void main(String[] args) {
TimeZone zone = TimeZone.getTimeZone("GMT+1");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
sdf.setTimeZone(zone);
TimeZone zone2 = TimeZone.getTimeZone("CET");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
sdf2.setTimeZone(zone2);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 15);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.setTimeZone(TimeZone.getTimeZone("GMT+3"));
System.out.println(sdf.format(c.getTime()));
System.out.println(sdf2.format(c.getTime()));
}
}
java.util.Date does not have a TimeZone, it's essentially a long (milliseconds since epoch). If you want to keep the timezone, you must use java.util.Calendar or even better, use joda-time
The second piece of code should do the trick.
Note that CET in java actually means CET in winter and CEST in summer which is what you want I assume. GMT+1 would not actually switch to summer time so you'd be stuck in winter time if you use that.
If the outputted value is still wrong you are giving it the wrong date to format.
Perhaps you made the same timezone error when parsing the date?
Avoid 3-Letter Codes
Those three-letter time zone codes are neither standardized nor unique. And they get confusing with regards to Daylight Saving Time (DST). Instead use proper time zone names.
There are a few dozen such names for +01:00. Choose the one that represents your applicable rules for DST and other anomalies. My example code arbitrarily chose Paris time zone.
Confusing Question
I could not understand if your input string represented a date-time at UTC or already in a +01:00 time zone. My example code below has two variations, covering both cases.
Also, you would have found your question already asked and answered many times on StackOverflow if you searched.
Joda-Time
The bundled java.util.Date and Calendar classes are notoriously troublesome. Avoid them. Use either:
Joda-Time
java.time.* package, new in Java 8(informed by Joda-Time, defined by JSR 310, and supplanting the old Date/Calendar classes)
Example Code
String input = "20140217 14:45:28";
// Formatters
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "yyyyMMdd HH:mm:ss" );
DateTimeFormatter formatterOutput = DateTimeFormat.forPattern( "dd.MM.yyyy HH:mm:ss" );
// Use a proper time zone name rather than 3-letter codes.
DateTimeZone timeZoneParis = DateTimeZone.forID( "Europe/Paris" );
// If that input was meant to be in UTC, and then adjusted to +01:00.
DateTime dateTimeAsUtc = formatterInput.withZone( DateTimeZone.UTC ).parseDateTime( input );
DateTime dateTimeAdjustedToParis = dateTimeAsUtc.withZone( timeZoneParis );
// Or, if that input was already in +01:00.
DateTime dateTimeAsParis = formatterInput.withZone( timeZoneParis ).parseDateTime( input );
Dump to console…
System.out.println( "input: " + input );
System.out.println( "dateTimeAsUtc: " + dateTimeAsUtc );
System.out.println( "dateTimeAdjustedToParis: " + dateTimeAdjustedToParis );
System.out.println( "dateTimeAdjustedToParis thru formatter: " + formatterOutput.print( dateTimeAdjustedToParis ) );
System.out.println( "dateTimeAsParis: " + dateTimeAsParis );
When run…
input: 20140217 14:45:28
dateTimeAsUtc: 2014-02-17T14:45:28.000Z
dateTimeAdjustedToParis: 2014-02-17T15:45:28.000+01:00
dateTimeAdjustedToParis thru formatter: 17.02.2014 15:45:28
dateTimeAsParis: 2014-02-17T14:45:28.000+01:00
I use the following code to get the date and time of my country;
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
Maybe it helps you, if it doesn't, just put a comment and i will delete my answer.

Handling timezone when parsing date in Java 6

I am trying to parse/validate the date 2013-06-19T12:00-05:00 using Java 6
I have tried several patterns, including following:
yyyy-MM-dd'T'HH:mmz
yyyy-MM-dd'T'HH:mmZ
yyyy-MM-dd'T'HH:mm'Z'
yyyy-MM-dd'T'HH:mm Z
yyyy-MM-dd'T'HH:mm z
yyyy-MM-dd'T'HH:mm'z'
yyyy-MM-dd'T'HH:mm-Z
yyyy-MM-dd'T'HH:mm-z
yyyy-MM-dd'T'hh:mm:ssZ
yyyy-mm-DD'T'hh:mm:ssZ
yyyy-MM-DD'T'hh:mm:ssZ
yyyy-MM-dd'T'HH:mm:ssZ
yyyy-MM-dd'T'HH:mm:ssz
but keep getting ParseException.
What would be the appropriate format/pattern for parsing 2013-06-19T12:00-05:00?
Thanks.
I would suggest that you use the excellent Joda-Time library to do this, specifically the parse(String str) method of the DateTime class, which will parse your example date using the default ISODateTimeFormat.dateTimeParser()
The JavaDoc for DateTime.parse(String str) is at http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#parse%28java.lang.String%29 and you can read more about Joda-Time at http://www.joda.org/joda-time/
String dateString = "2013-06-19T12:00-05:00";
String pattern = "yyyy-MM-dd'T'HH:mmZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
Date ans=dateTime.toDate();
System.out.println(ans);
Use simpledateformat with pattern like "yyyy-MM-dd'T'HH-mm:ss:SS"
The Joda-Time 2.3 library is already built to handle that variation of ISO 8601 format. No need to create a formatter.
I arbitrarily chose Montréal Québec as a time zone because of your -05:00 time zone offset. You may rather work with UTC/GMT date-times in which case you can pass a pre-defined instance: DateTimeZone.UTC.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
String dateTimeString = "2013-06-19T12:00-05:00";
DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" ); // Or for UTC/GMT, use: DateTimeZone.UTC
DateTime dateTime = new DateTime( dateTimeString, timeZone );
Dump to console…
System.out.println( "dateTimeString: " + dateTimeString );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime in UTC/GMT: " + dateTime.toDateTime( DateTimeZone.UTC ) );
When run…
dateTimeString: 2013-06-19T12:00-05:00
dateTime: 2013-06-19T13:00:00.000-04:00
dateTime in UTC/GMT: 2013-06-19T17:00:00.000Z
Note the one hour difference because Montréal Québec was in Daylight Saving Time (DST) on that date.
The date 2013-06-19T12:00-05:00 can't be parsed under Java 6 in current form. On other hand using Joda-Time seems to be an overkill.
To solve problem I added timezone to the date:
public Date extractDate(String dateStr) {
Pattern aPattern = Pattern.compile("\\d+([+-]\\d+:\\d+)$", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher matcher = aPattern.matcher(dateStr);
if(matcher.find()){
String timezone = matcher.group(1);
System.out.println("timezone:" + timezone);
dateStr = StringUtils.replace(dateStr, timezone, "GMT" + timezone);
}
Date date = null;
String [] datePatterns = {"yyyy-MM-dd'T'HH:mmZ"};
try {
date = DateUtils.parseDateStrictly(dateStr, datePatterns);
}
catch (Exception except) {
except.printStackTrace();
}
return date;
}

Date in to UTC format Java

I have a string like this 2013-10-22T01:37:56. I Need to change this string into UTC Date format like this MM/dd/yyyy KK:mm:ss a. I have tried some code but it is not returning the UTC datetime.
My code is
String[] time = itsAlarmDttm.split("T");
String aFormatDate = time[0]+ " "+time[1];
String aRevisedDate = null;
try {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date dateObj = sdf.parse(aFormatDate);
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
} catch (ParseException e) {
itsLogger.error("Error occured in Parsing the Data Time Object: " +e.getMessage());
} catch (Exception e) {
itsLogger.error("Error occured in Data Time Objecct: " +e.getMessage());
}
I am getting the output is MM/dd/yyyy KK:mm:ss a format. But Not UTC time format.
How to solve this issue?
Try this... Worked for me and printed 10/22/2013 01:37:56 AM Ofcourse this is your code only with little modifications.
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // This line converts the given date into UTC time zone
final java.util.Date dateObj = sdf.parse("2013-10-22T01:37:56");
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
Try to format your date with the Z or z timezone flags:
new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a Z").format(dateObj);
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
// or SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy KK:mm:ss a Z" );
sdf.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
System.out.println( sdf.format( new Date() ) );
What Time Zones?
No where in your question do you mention time zone. What time zone is implied that input string? What time zone do you want for your output? And, UTC is a time zone (or lack thereof depending on your mindset) not a string format.
ISO 8601
Your input string is in ISO 8601 format, except that it lacks an offset from UTC.
Joda-Time
Here is some example code in Joda-Time 2.3 to show you how to handle time zones. Joda-Time has built-in default formatters for parsing and generating String representations of date-time values.
String input = "2013-10-22T01:37:56";
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
DateTime dateTimeMontréal = dateTimeUtc.withZone( DateTimeZone.forID( "America/Montreal" );
String output = dateTimeMontréal.toString();
As for generating string representations in other formats, search StackOverflow for "Joda format".
java.time
It’s about time someone provides the modern answer. The modern solution uses java.time, the modern Java date and time API. The classes SimpleDateFormat and Date used in the question and in a couple of the other answers are poorly designed and long outdated, the former in particular notoriously troublesome. TimeZone is poorly designed to. I recommend you avoid those.
ZoneId utc = ZoneId.of("Etc/UTC");
DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern(
"MM/dd/yyyy hh:mm:ss a zzz", Locale.ENGLISH);
String itsAlarmDttm = "2013-10-22T01:37:56";
ZonedDateTime utcDateTime = LocalDateTime.parse(itsAlarmDttm)
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(utc);
String formatterUtcDateTime = utcDateTime.format(targetFormatter);
System.out.println(formatterUtcDateTime);
When running in my time zone, Europe/Copenhagen, the output is:
10/21/2013 11:37:56 PM UTC
I have assumed that the string you got was in the default time zone of your JVM, a fragile assumption since that default setting can be changed at any time from another part of your program or another programming running in the same JVM. If you can, instead specify time zone explicitly, for example ZoneId.of("Europe/Podgorica") or ZoneId.of("Asia/Kolkata").
I am exploiting the fact that you string is in ISO 8601 format, the format the the modern classes parse as their default, that is, without any explicit formatter.
I am using a ZonedDateTime for the result date-time because it allows us to format it with UTC in the formatted string to eliminate any and all doubt. For other purposes one would typically have wanted an OffsetDateTime or an Instant instead.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601

Date localization in java using DateFormat.getDateInstance

I have a date which must be localized.
Below code returns 5/1/12 19:06:34 but the result i want is 05/01/12 19:06:34
Could you please tell me how to manage this.
private String localizeDate(String date){ //Format is 2012-05-01 19:30:49
Locale loc = DataContextHolder.getDataContext().getLocale();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", loc);
Date parsed=null;
try {
parsed = formatter.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);
String localizedDate = df.format(parsed) + " " + date.substring(11, 13) + ":"
+ date.substring(14, 16) + ":"
+ date.substring(17, 19);
return localizedDate;
}
You can avoid leading zeros by reducing the number of consecutive pattern letters for that particular element. Multiple pattern letters in a row tell the date formatter that, at minimum, you want that many characters to express that value.
In your example, the following should resolve your problem.
new SimpleDateFormat("y-M-d H:m:s", loc);
Find more in the SimpleDateFormat documentation.
For clarity, see the following example.
SimpleDateFormat a = new SimpleDateFormat("yyyyy-MMMM-dddd HHH:mmmm:sssss");
SimpleDateFormat b = new SimpleDateFormat("y-M-d H:m:s");
System.out.println(a.format(new Date()));
// Prints 02012-June-0005 012:0027:00026
System.out.println(b.format(new Date()));
// Prints 12-6-5 12:27:26
Problems
You have multiple problems happening with your code.
Time-Zone
One problem is that you are localizing without handling time zone. The parsing will apply the JVM's default time zone. That means running on different computers with different time zone settings yields different results. Generally best to specify a time zone rather than rely on default.
Formatting, Not Localizing
Another problem is that if you are truly localizing, you would not be specifying the details of the format. You would let the locale drive the formatting, whatever is appropriate to that locale and to the user's own localization settings.
Avoid java.util.Date
Yet another problem is that you are using the java.util.Date class bundled with Java. That class, and its sibling java.util.Calendar, are notoriously troublesome. Avoid them. Use either the Joda-Time framework or the new java.time.* package in Java 8.
ISO 8601
Your input string is very close to the standard ISO 8601 format, like this: 2014-02-07T07:06:41+03:00. Your text is merely missing a T in the middle and a time zone offset.
The Joda-Time framework uses ISO 8601 by default. So replacing that space with a "T" lets you pass the string directly to a DateTime constructor. No need for a formatter and parsing, as the DateTime constructor will do that work for you automatically.
Note that including or omitting the time zone offset from the end of the string input changes the behavior of constructing a DateTime. Without an offset, the string is parsed as if it occurred at the specified time within the passed time zone (the 2nd argument, see code example below). On the other hand, if you do include an offset in your input, the string is parsed as if it occurred at the specified time in the offset's locality, and then adjusts the time to the passed time zone argument.
Joda-Time Example
Here is some example code in Joda-Time 2.3.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Istanbul" );
String inputOriginal = "2012-05-01 19:30:49";
String input = inputOriginal.replace( " ", "T" );
DateTime dateTime = new DateTime( input, timeZone );
// Or if your input is UTC/GMT (no time zone offset), pass a predefined DateTimeZone constant.
//DateTime dateTime = new DateTime( input, DateTimeZone.UTC );
We have our date-time value in hand. Now we proceed to generate formatted strings from that date-time. We do so by using Joda-Time’s Locale-sensitive formatting facility. Note that not only the format is localized, so is the text of the month and day names localized to appropriate language.
// Create a formatter from a two-character style pattern.
// The first character is the date style, and the second character is the time style.
// Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full.
// A date or time may be ommitted by specifying a style character '-'.
java.util.Locale locale = new java.util.Locale( "tr", "TR" ); // Turkey chosen as an example.
String output_ShortShort = DateTimeFormat.forStyle( "SS" ).withLocale( locale ).print( dateTime );
String output_LongShort = DateTimeFormat.forStyle( "LS" ).withLocale( locale ).print( dateTime );
Dump to console…
System.out.println( "input: " + input );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime in UTC/GMT: " + dateTime.toDateTime( DateTimeZone.UTC ) );
System.out.println( "output_ShortShort: " + output_ShortShort );
System.out.println( "output_LongShort: " + output_LongShort );
When run…
input: 2012-05-01T19:30:49
dateTime: 2012-05-01T19:30:49.000+03:00
dateTime in UTC/GMT: 2012-05-01T16:30:49.000Z
output_ShortShort: 01.05.2012 19:30
output_LongShort: 01 Mayıs 2012 Salı 19:30
You need to choose the appropriate date formatter. Please read the documentation here:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Categories

Resources