rakeeee
rakeeee

Reputation: 1073

Converting UTC Timestamp to Any timeZone

I am trying to convert UTC date time stamp to any Timezone but i always end up with UTC date time in both cases.

 import java.sql.Timestamp;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.TimeZone;

  public class UTCtoLocalTime {

private static String ConvertToLocalTime(String id, String time) throws ParseException{
    DateFormat localtime = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    localtime.setTimeZone(TimeZone.getTimeZone(id));
    return localtime.format(localtime.parse(time));
}

public static void main(String[] args) throws ParseException {
    SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));

    String time =  dateFormatGmt.format(new Timestamp(System.currentTimeMillis()));

    System.out.println("UTC time "+ time);
    System.out.println("Local Time "+ ConvertToLocalTime("America/Mexico_City", time));
}

}

I am not sure where i am doing mistake. Can any one help me?

Upvotes: 0

Views: 216

Answers (3)

Adrian Shum
Adrian Shum

Reputation: 40056

Easy to in JODA time:

Assuming you have a DateTime storing the incoming date time which in UTC, the way to get the string presentation of it for another timezone is merely:

String result = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss")
                               .withZone(DateTimeZone.forID(timeZoneId))
                               .print(utcDateTime);

Different combination of String , LocalDateTime and Datetime conversion between UTC and another timezone are all very easy

Upvotes: 0

rakeeee
rakeeee

Reputation: 1073

Here i use this to serve my purpose

public static String UTCtolocaltime(String timzoneid, String UTCTime){
        String convertedlocaldatetime;
        Date datetimeinutc = null;
        SimpleDateFormat utcdateFormat = new SimpleDateFormat(some patteren); 
        utcdateFormat.setTimeZone(TimeZone.getTimeZone(UTC));
        try {
            datetimeinutc = utcdateFormat.parse(UTCTime);
        } catch (ParseException e1) {
            log.error(e1);
        }
        SimpleDateFormat localdateFormat = new SimpleDateFormat(some patteren); 
        localdateFormat.setTimeZone(TimeZone.getTimeZone(timzoneid));
             convertedlocaldatetime = localdateFormat.format(datetimeinutc);
             return convertedlocaldatetime;
    }

Upvotes: 0

Ankur Anand
Ankur Anand

Reputation: 3904

Date doesn't has a time zone. In String time you are displaying a Date in some way and expecting that display to use some time zone other . It won't.

String time represents a timestamp in one time zone, and you want to change that to a string which represents a timestamp in another time zone,

So basically you have

Create another SimpleDateFormat and apply the second time zone to it Use that to format the Date object to a new String

Here is implemented Code

public class TestClass {
        private static String ConvertToLocalTime(String id, String time) throws ParseException{
            DateFormat localtime = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            localtime.setTimeZone(TimeZone.getTimeZone(id));
            return localtime.format(localtime.parse(time));
        }

        public static void main(String[] args) throws ParseException {
            SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));

            String time =  dateFormatGmt.format(new Timestamp(System.currentTimeMillis()));
            System.out.println("UTC time "+ time);

            SimpleDateFormat dateFormatGmt1 = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            String time1 =  dateFormatGmt1.format(new Timestamp(System.currentTimeMillis()));
            System.out.println("Local Time "+ ConvertToLocalTime("America/Mexico_City", time1));
        }

        }

OutPut on my Machine

UTC time 2015-Jun-16 15:45:16
Local Time 2015-Jun-16 21:15:16

Upvotes: 1

Related Questions