LK Yeung
LK Yeung

Reputation: 3496

the simplest way to change a time to particular timezone

there are two string

String date = "9/13/2012";
String time = "5:48pm";

the time is GMT+0, I wanna change it to GMT+8,what is the simplest way to change a time to particular timezone

Upvotes: 0

Views: 214

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String date = "9/13/2012";
        String time = "5:48pm";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/u h:ma", Locale.UK);
        LocalDateTime ldtSource = LocalDateTime.parse(date + " " + time, dtf);

        OffsetDateTime odtSource = ldtSource.atOffset(ZoneOffset.UTC);
        OffsetDateTime odtTarget = odtSource.withOffsetSameInstant(ZoneOffset.of("+08:00"));

        System.out.println(odtTarget);

        // In a custom format
        System.out.println(odtTarget.format(dtf));
    }
}

Output:

2012-09-14T01:48+08:00
9/14/2012 1:48am

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Upvotes: 1

richardr
richardr

Reputation: 5192

The Joda-Time library provides a good set of objects for working with dates/times in multiple time zones. http://joda-time.sourceforge.net/

Something like this for example:

    String date = "9/13/2012";
    String time = "5:48pm";

    String[] dateParts = date.split("/");
    Integer month = Integer.parseInt(dateParts[0]);
    Integer day = Integer.parseInt(dateParts[1]);
    Integer year = Integer.parseInt(dateParts[2]);

    String[] timeParts = time.split(":");
    Integer hour = Integer.parseInt(timeParts[0]);
    Integer minutes = Integer.parseInt(timeParts[1].substring(0,timeParts[1].lastIndexOf("p")));

    DateTime dateTime = new DateTime(year, month, day, hour, minutes, DateTimeZone.forID("Etc/GMT"));
    dateTime.withZone(DateTimeZone.forID("Etc/GMT+8"));

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1504182

  • Parse it using a SimpleDateFormat set to the UTC time zone
  • Format the parsed Date value using a SimpleDateFormat set to the time zone you're interested in. (It's likely to be something other than just "UTC+8" - you should find out which TZDB time zone ID you really want.

For example:

SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy h:mma", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC");
Date date = inputFormat.parse(date + " " + time);

// Or whatever format you want...
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
outputFormat.setTimeZone(targetTimeZone);
String outputText = outputFormat.format(date);

(If you can use Joda Time instead, that'd be great - but I understand that it's pretty big for an Android app.)

Upvotes: 2

Related Questions