Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    private static final String TIME_ZONE_PRC = "PRC";
    private static final String STAMP_TO_STRING = "yyyy-MM-dd HH:mm:ss.SSSZ";
    private static final String STAMP_TO_DATE = "yyyy-MM-dd";

    public static String StampToDate(long stamp) {
        return StampToString(stamp).substring(0, STAMP_TO_DATE.length());
    }

    public static String StampToString(long stamp) {
        /*
         String[] formats = new String[] {  
         "yyyy-MM-dd",   
         "yyyy-MM-dd HH:mm",  
         "yyyy-MM-dd HH:mmZ",   
         "yyyy-MM-dd HH:mm:ss.SSSZ",
         "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
         }; 
         */
        Date date = null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(stamp);
        date = calendar.getTime();

        String format = STAMP_TO_STRING;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_PRC));

        return sdf.format(date);

    }
}