2

i found many functions to convert from utc to local time but i didnot find how to convert from local to utc ?

this is used from utc to local ,how to edit to get utc from local ?

     public static String timezoneAwareDate(String date){
    // create simpledateformat for UTC dates in database
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date output;
    // parse time
    try{
        output = simpleDateFormat.parse(date);
    }catch (Exception e){
        // return current time
        output = new Date();
    }

    return output.toString();
}
Mina Farid
  • 5,041
  • 4
  • 39
  • 46

2 Answers2

2
public static java.util.Date getConvertedDate(java.util.Date date,String newTimeZone) {

    if(date != null) {
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy hh:mm a");
        df.setTimeZone(TimeZone.getTimeZone(newTimeZone));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        String newdt = df.format(calendar.getTime());
        try {
            date = df.parse(newdt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return date;
}
  • 3
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Ferrybig Feb 29 '16 at 14:16
1

Add this class in your project and use their methods for local to utc or utc to local.

import java.util.TimeZone;

public final class Utility {
public static final TimeZone utcTZ = TimeZone.getTimeZone("UTC");

public static long toLocalTime(long time, TimeZone to) {
    return convertTime(time, utcTZ, to);
}

public static long toUTC(long time, TimeZone from) {
    return convertTime(time, from, utcTZ);
}

public static long convertTime(long time, TimeZone from, TimeZone to) {
    return time + getTimeZoneOffset(time, from, to);
}

private static long getTimeZoneOffset(long time, TimeZone from, TimeZone to)    {
    int fromOffset = from.getOffset(time);
    int toOffset = to.getOffset(time);
    int diff = 0;

    if (fromOffset >= 0){
        if (toOffset > 0){
            toOffset = -1*toOffset;
        } else {
            toOffset = Math.abs(toOffset);
        }
        diff = (fromOffset+toOffset)*-1;
    } else {
        if (toOffset <= 0){
            toOffset = -1*Math.abs(toOffset);
        }
        diff = (Math.abs(fromOffset)+toOffset);
    }
    return diff;
   }
}

if you want to more about this thing. It is already answered in this link

Community
  • 1
  • 1
slymnozdmrc
  • 390
  • 2
  • 8
  • 20
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Ferrybig Feb 29 '16 at 14:20
  • Functionality aside, `Utility` is a really vague name for a class – Tim Feb 29 '16 at 14:24
  • @TimCastelijns you are right but this class can be extend more method to solve more thing. Have you got any suggestion as name for this class :) – slymnozdmrc Feb 29 '16 at 14:27