get GMT8 Full Date Time - Android java.util

Android examples for java.util:Time

Description

get GMT8 Full Date Time

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    public static Date getGMT8FullDateTime(String sDate) {
        try {/*w  w w  . jav  a 2 s.c  o m*/
            String formater = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            SimpleDateFormat format = new SimpleDateFormat(formater);
            Date date = format.parse(sDate);

            Calendar gmtlocal = new GregorianCalendar(
                    TimeZone.getTimeZone("GMT+0"));
            gmtlocal.setTime(date);

            SimpleDateFormat sf = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");

            sf.setTimeZone(TimeZone.getTimeZone("GMT+16"));
            return sf.parse(sf.format(gmtlocal.getTime()));
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Date format(String s, String forma) {
        Date lastdt = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(forma);
            lastdt = sdf.parse(s);
            return lastdt;
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Date format(String s) {
        Date lastdt = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            lastdt = sdf.parse(s);
            return lastdt;
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Date format(Date s, String forma) {
        Date lastdt = null;
        String date = getDateTime(s, forma);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(forma);
            lastdt = sdf.parse(date);
            return lastdt;
        } catch (Exception e) {
            System.out.println("s");
            return new Date();
        }
    }

    public static String getDateTime(Date sDate, String forma) {
        if (sDate == null)
            return "";
        try {
            return new SimpleDateFormat(forma).format(sDate);
        } catch (Exception ex) {
            return "";
        }
    }

    public static String getDateTime(Date sDate) {
        if (sDate == null)
            return "";
        try {
            return new SimpleDateFormat("yyyy-MM-dd").format(sDate);
        } catch (Exception ex) {
            return "";
        }
    }
}

Related Tutorials