get Beijing Date time - Java java.util

Java examples for java.util:Timezone

Description

get Beijing Date time

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getBeijingDatetime());
    }/*w  ww  . j  a v a  2s.c om*/

    public static Date getBeijingDatetime() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
            return sdf.parse(sdf.format(d));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials