get GMT Now - Android java.util

Android examples for java.util:Date

Description

get GMT Now

Demo Code

import android.content.Context;
import android.text.format.DateFormat;
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 final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
    public static Date getGMTNow() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        try {//from  w  w  w.j a v  a  2  s .  com
            return sdf.parse(getGMTStringNow());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String getGMTStringNow() {
      Calendar cal = Calendar.getInstance();
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
      return sdf.format(date);
  }
}

Related Tutorials