문제

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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top