Java Longタイプのタイムスタンプを年月日週に変更

5742 ワード

Java Longタイプのタイムスタンプを年月日週に変更
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeTransform {
    /**
     *    
     *
     * @param time
     * long      long  
     * @return        
     *
     * */
    public static String getWeekOfDate(long time) {

        Date date = new Date(time);
        String[] weekDays = { "   ", "   ", "   ", "   ", "   ", "   ", "   " };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     *    - -  hh:ff:ss    
     *
     * @param time
     * long    
     * @return String   2013-05-26 19:39:26    
     *
     * */

    public static String getDateAndWeek(long time) {

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

        Date date = new Date(time);

        String dateString = format.format(date);

        String dayString = getWeekOfDate(time);

        return dateString + " " + dayString;

    }
}