Android Timezone Change serverTimeStringToLocalDateTime( String serverTimeString, String serverTimeFormat)

Here you can find the source of serverTimeStringToLocalDateTime( String serverTimeString, String serverTimeFormat)

Description

server Time String To Local Date Time

License

Open Source License

Declaration

public static Date serverTimeStringToLocalDateTime(
            String serverTimeString, String serverTimeFormat) 

Method Source Code

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static Date serverTimeStringToLocalDateTime(
            String serverTimeString, String serverTimeFormat) {
        if (serverTimeString.equals("") || serverTimeString.equals(null)) {
            return null;
        }//from   ww w .j a v a 2s .  co  m
        SimpleDateFormat sd = new SimpleDateFormat(serverTimeFormat);
        sd.setTimeZone(TimeZone.getTimeZone("UTC"));

        Calendar localCal = Calendar.getInstance();
        Date date = new Date();
        try {
            date = sd.parse(serverTimeString);

            localCal.setTime(date);
            localCal.setTimeZone(TimeZone.getDefault());
            Date dateLocal = localCal.getTime();
            return dateLocal;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. localDateTimeToServerTimeString( Date localDateTime, String serverTimeFormat)
  2. getLocalCalendar()