Android 時間工具類 時間戳與日期間的各種互換

  1. package com.pts.peoplehui.utils;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6. import java.util.Locale;  
  7.   
  8. public class DateUtils {  
  9.   
  10.     public static String getTodayDateTime() {  
  11.         SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”,  
  12.                 Locale.getDefault());  
  13.         return format.format(new Date());  
  14.     }  
  15.   
  16.     /** 
  17.      * 掉此方法輸入所要轉換的時間輸入例如(”2014年06月14日16時09分00秒”)返回時間戳 
  18.      *  
  19.      * @param time 
  20.      * @return 
  21.      */  
  22.     public String data(String time) {  
  23.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日HH時mm分ss秒”,  
  24.                 Locale.CHINA);  
  25.         Date date;  
  26.         String times = null;  
  27.         try {  
  28.             date = sdr.parse(time);  
  29.             long l = date.getTime();  
  30.             String stf = String.valueOf(l);  
  31.             times = stf.substring(010);  
  32.         } catch (Exception e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         return times;  
  36.     }  
  37.   
  38.     public static String getTodayDateTimes() {  
  39.         SimpleDateFormat format = new SimpleDateFormat(“MM月dd日”,  
  40.                 Locale.getDefault());  
  41.         return format.format(new Date());  
  42.     }  
  43.       
  44.     /** 
  45.      * 獲取當前時間 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public static String getCurrentTime_Today() {  
  50.         SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);  
  51.         return sdf.format(new java.util.Date());  
  52.     }  
  53.   
  54.     /** 
  55.      * 調此方法輸入所要轉換的時間輸入例如(”2014-06-14-16-09-00”)返回時間戳 
  56.      *  
  57.      * @param time 
  58.      * @return 
  59.      */  
  60.     public static String dataOne(String time) {  
  61.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”,  
  62.                 Locale.CHINA);  
  63.         Date date;  
  64.         String times = null;  
  65.         try {  
  66.             date = sdr.parse(time);  
  67.             long l = date.getTime();  
  68.             String stf = String.valueOf(l);  
  69.             times = stf.substring(010);  
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         }  
  73.         return times;  
  74.     }  
  75.   
  76.     public static String getTimestamp(String time, String type) {  
  77.         SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA);  
  78.         Date date;  
  79.         String times = null;  
  80.         try {  
  81.             date = sdr.parse(time);  
  82.             long l = date.getTime();  
  83.             String stf = String.valueOf(l);  
  84.             times = stf.substring(010);  
  85.         } catch (Exception e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         return times;  
  89.     }  
  90.   
  91.     /** 
  92.      * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出(”2014年06月14日16時09分00秒”) 
  93.      *  
  94.      * @param time 
  95.      * @return 
  96.      */  
  97.     public static String times(String time) {  
  98.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日HH時mm分ss秒”);  
  99.         @SuppressWarnings(“unused”)  
  100.         long lcc = Long.valueOf(time);  
  101.         int i = Integer.parseInt(time);  
  102.         String times = sdr.format(new Date(i * 1000L));  
  103.         return times;  
  104.   
  105.     }  
  106.       
  107.     /** 
  108.      * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出(”2014-06-14  16:09:00”) 
  109.      *  
  110.      * @param time 
  111.      * @return 
  112.      */  
  113.     public static String timedate(String time) {  
  114.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);  
  115.         @SuppressWarnings(“unused”)  
  116.         long lcc = Long.valueOf(time);  
  117.         int i = Integer.parseInt(time);  
  118.         String times = sdr.format(new Date(i * 1000L));  
  119.         return times;  
  120.   
  121.     }  
  122.   
  123.     /** 
  124.      * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出(”2014年06月14日16:09”) 
  125.      *  
  126.      * @param time 
  127.      * @return 
  128.      */  
  129.     public static String timet(String time) {  
  130.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日  HH:mm”);  
  131.         @SuppressWarnings(“unused”)  
  132.         long lcc = Long.valueOf(time);  
  133.         int i = Integer.parseInt(time);  
  134.         String times = sdr.format(new Date(i * 1000L));  
  135.         return times;  
  136.   
  137.     }  
  138.   
  139.     /** 
  140.      * @param time斜槓分開 
  141.      * @return 
  142.      */  
  143.     public static String timeslash(String time) {  
  144.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy/MM/dd,HH:mm”);  
  145.         @SuppressWarnings(“unused”)  
  146.         long lcc = Long.valueOf(time);  
  147.         int i = Integer.parseInt(time);  
  148.         String times = sdr.format(new Date(i * 1000L));  
  149.         return times;  
  150.   
  151.     }  
  152.   
  153.     /** 
  154.      * @param time斜槓分開 
  155.      * @return 
  156.      */  
  157.     public static String timeslashData(String time) {  
  158.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy/MM/dd”);  
  159.         @SuppressWarnings(“unused”)  
  160.         long lcc = Long.valueOf(time);  
  161. //      int i = Integer.parseInt(time);  
  162.         String times = sdr.format(new Date(lcc * 1000L));  
  163.         return times;  
  164.   
  165.     }  
  166.       
  167.     /** 
  168.      * @param time斜槓分開 
  169.      * @return 
  170.      */  
  171.     public static String timeMinute(String time) {  
  172.         SimpleDateFormat sdr = new SimpleDateFormat(“HH:mm”);  
  173.         @SuppressWarnings(“unused”)  
  174.         long lcc = Long.valueOf(time);  
  175.         int i = Integer.parseInt(time);  
  176.         String times = sdr.format(new Date(i * 1000L));  
  177.         return times;  
  178.   
  179.     }  
  180.   
  181.     public static String tim(String time) {  
  182.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyyMMdd HH:mm”);  
  183.         @SuppressWarnings(“unused”)  
  184.         long lcc = Long.valueOf(time);  
  185.         int i = Integer.parseInt(time);  
  186.         String times = sdr.format(new Date(i * 1000L));  
  187.         return times;  
  188.     }  
  189.   
  190.     public static String time(String time) {  
  191.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);  
  192.         @SuppressWarnings(“unused”)  
  193.         long lcc = Long.valueOf(time);  
  194.         int i = Integer.parseInt(time);  
  195.         String times = sdr.format(new Date(i * 1000L));  
  196.         return times;  
  197.     }  
  198.   
  199.     // 調用此方法輸入所要轉換的時間戳例如(1402733340)輸出(”2014年06月14日16時09分00秒”)  
  200.     public static String times(long timeStamp) {  
  201.         SimpleDateFormat sdr = new SimpleDateFormat(“MM月dd日  #  HH:mm”);  
  202.         return sdr.format(new Date(timeStamp)).replaceAll(“#”,  
  203.                 getWeek(timeStamp));  
  204.   
  205.     }  
  206.   
  207.     private static String getWeek(long timeStamp) {  
  208.         int mydate = 0;  
  209.         String week = null;  
  210.         Calendar cd = Calendar.getInstance();  
  211.         cd.setTime(new Date(timeStamp));  
  212.         mydate = cd.get(Calendar.DAY_OF_WEEK);  
  213.         // 獲取指定日期轉換成星期幾  
  214.         if (mydate == 1) {  
  215.             week = ”週日”;  
  216.         } else if (mydate == 2) {  
  217.             week = ”週一”;  
  218.         } else if (mydate == 3) {  
  219.             week = ”週二”;  
  220.         } else if (mydate == 4) {  
  221.             week = ”週三”;  
  222.         } else if (mydate == 5) {  
  223.             week = ”週四”;  
  224.         } else if (mydate == 6) {  
  225.             week = ”週五”;  
  226.         } else if (mydate == 7) {  
  227.             week = ”週六”;  
  228.         }  
  229.         return week;  
  230.     }  
  231.   
  232.     // 並用分割符把時間分成時間數組  
  233.     /** 
  234.      * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出(”2014-06-14-16-09-00”) 
  235.      *  
  236.      * @param time 
  237.      * @return 
  238.      */  
  239.     public String timesOne(String time) {  
  240.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);  
  241.         @SuppressWarnings(“unused”)  
  242.         long lcc = Long.valueOf(time);  
  243.         int i = Integer.parseInt(time);  
  244.         String times = sdr.format(new Date(i * 1000L));  
  245.         return times;  
  246.   
  247.     }  
  248.   
  249.     public static String timesTwo(String time) {  
  250.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd”);  
  251.         @SuppressWarnings(“unused”)  
  252.         long lcc = Long.valueOf(time);  
  253.         int i = Integer.parseInt(time);  
  254.         String times = sdr.format(new Date(i * 1000L));  
  255.         return times;  
  256.   
  257.     }  
  258.   
  259.     /** 
  260.      * 並用分割符把時間分成時間數組 
  261.      *  
  262.      * @param time 
  263.      * @return 
  264.      */  
  265.     public static String[] timestamp(String time) {  
  266.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日HH時mm分ss秒”);  
  267.         @SuppressWarnings(“unused”)  
  268.         long lcc = Long.valueOf(time);  
  269.         int i = Integer.parseInt(time);  
  270.         String times = sdr.format(new Date(i * 1000L));  
  271.         String[] fenge = times.split(”[年月日時分秒]”);  
  272.         return fenge;  
  273.     }  
  274.   
  275.     /** 
  276.      * 根據傳遞的類型格式化時間 
  277.      *  
  278.      * @param str 
  279.      * @param type 
  280.      *            例如:yy-MM-dd 
  281.      * @return 
  282.      */  
  283.     public static String getDateTimeByMillisecond(String str, String type) {  
  284.   
  285.         Date date = new Date(Long.valueOf(str));  
  286.   
  287.         SimpleDateFormat format = new SimpleDateFormat(type);  
  288.   
  289.         String time = format.format(date);  
  290.   
  291.         return time;  
  292.     }  
  293.   
  294.     /** 
  295.      * 分割符把時間分成時間數組 
  296.      *  
  297.      * @param time 
  298.      * @return 
  299.      */  
  300.     public String[] division(String time) {  
  301.   
  302.         String[] fenge = time.split(”[年月日時分秒]”);  
  303.   
  304.         return fenge;  
  305.   
  306.     }  
  307.   
  308.     /** 
  309.      * 輸入時間戳變星期 
  310.      *  
  311.      * @param time 
  312.      * @return 
  313.      */  
  314.     public static String changeweek(String time) {  
  315.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日HH時mm分ss秒”);  
  316.         long lcc = Long.valueOf(time);  
  317.         int i = Integer.parseInt(time);  
  318.         String times = sdr.format(new Date(i * 1000L));  
  319.         Date date = null;  
  320.         int mydate = 0;  
  321.         String week = null;  
  322.         try {  
  323.             date = sdr.parse(times);  
  324.             Calendar cd = Calendar.getInstance();  
  325.             cd.setTime(date);  
  326.             mydate = cd.get(Calendar.DAY_OF_WEEK);  
  327.             // 獲取指定日期轉換成星期幾  
  328.         } catch (Exception e) {  
  329.             // TODO Auto-generated catch block  
  330.             e.printStackTrace();  
  331.         }  
  332.         if (mydate == 1) {  
  333.             week = ”星期日”;  
  334.         } else if (mydate == 2) {  
  335.             week = ”星期一”;  
  336.         } else if (mydate == 3) {  
  337.             week = ”星期二”;  
  338.         } else if (mydate == 4) {  
  339.             week = ”星期三”;  
  340.         } else if (mydate == 5) {  
  341.             week = ”星期四”;  
  342.         } else if (mydate == 6) {  
  343.             week = ”星期五”;  
  344.         } else if (mydate == 7) {  
  345.             week = ”星期六”;  
  346.         }  
  347.         return week;  
  348.   
  349.     }  
  350.   
  351.     /** 
  352.      * 獲取日期和星期 例如:2014-11-13 11:00 星期一 
  353.      *  
  354.      * @param time 
  355.      * @param type 
  356.      * @return 
  357.      */  
  358.     public static String getDateAndWeek(String time, String type) {  
  359.         return getDateTimeByMillisecond(time + “000”, type) + “  ”  
  360.                 + changeweekOne(time);  
  361.     }  
  362.   
  363.     /** 
  364.      * 輸入時間戳變星期 
  365.      *  
  366.      * @param time 
  367.      * @return 
  368.      */  
  369.     public static String changeweekOne(String time) {  
  370.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);  
  371.         long lcc = Long.valueOf(time);  
  372.         int i = Integer.parseInt(time);  
  373.         String times = sdr.format(new Date(i * 1000L));  
  374.         Date date = null;  
  375.         int mydate = 0;  
  376.         String week = null;  
  377.         try {  
  378.             date = sdr.parse(times);  
  379.             Calendar cd = Calendar.getInstance();  
  380.             cd.setTime(date);  
  381.             mydate = cd.get(Calendar.DAY_OF_WEEK);  
  382.             // 獲取指定日期轉換成星期幾  
  383.         } catch (Exception e) {  
  384.             // TODO Auto-generated catch block  
  385.             e.printStackTrace();  
  386.         }  
  387.         if (mydate == 1) {  
  388.             week = ”星期日”;  
  389.         } else if (mydate == 2) {  
  390.             week = ”星期一”;  
  391.         } else if (mydate == 3) {  
  392.             week = ”星期二”;  
  393.         } else if (mydate == 4) {  
  394.             week = ”星期三”;  
  395.         } else if (mydate == 5) {  
  396.             week = ”星期四”;  
  397.         } else if (mydate == 6) {  
  398.             week = ”星期五”;  
  399.         } else if (mydate == 7) {  
  400.             week = ”星期六”;  
  401.         }  
  402.         return week;  
  403.   
  404.     }  
  405.   
  406.     /** 
  407.      * 獲取當前時間 
  408.      *  
  409.      * @return 
  410.      */  
  411.     public static String getCurrentTime() {  
  412.         SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);  
  413.         return sdf.format(new java.util.Date());  
  414.     }  
  415.       
  416.     /** 
  417.      * 輸入日期如(2014年06月14日16時09分00秒)返回(星期數) 
  418.      *  
  419.      * @param time 
  420.      * @return 
  421.      */  
  422.     public String week(String time) {  
  423.         Date date = null;  
  424.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日HH時mm分ss秒”);  
  425.         int mydate = 0;  
  426.         String week = null;  
  427.         try {  
  428.             date = sdr.parse(time);  
  429.             Calendar cd = Calendar.getInstance();  
  430.             cd.setTime(date);  
  431.             mydate = cd.get(Calendar.DAY_OF_WEEK);  
  432.             // 獲取指定日期轉換成星期幾  
  433.         } catch (Exception e) {  
  434.             // TODO Auto-generated catch block  
  435.             e.printStackTrace();  
  436.         }  
  437.         if (mydate == 1) {  
  438.             week = ”星期日”;  
  439.         } else if (mydate == 2) {  
  440.             week = ”星期一”;  
  441.         } else if (mydate == 3) {  
  442.             week = ”星期二”;  
  443.         } else if (mydate == 4) {  
  444.             week = ”星期三”;  
  445.         } else if (mydate == 5) {  
  446.             week = ”星期四”;  
  447.         } else if (mydate == 6) {  
  448.             week = ”星期五”;  
  449.         } else if (mydate == 7) {  
  450.             week = ”星期六”;  
  451.         }  
  452.         return week;  
  453.     }  
  454.   
  455.     /** 
  456.      * 輸入日期如(2014-06-14-16-09-00)返回(星期數) 
  457.      *  
  458.      * @param time 
  459.      * @return 
  460.      */  
  461.     public String weekOne(String time) {  
  462.         Date date = null;  
  463.         SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);  
  464.         int mydate = 0;  
  465.         String week = null;  
  466.         try {  
  467.             date = sdr.parse(time);  
  468.             Calendar cd = Calendar.getInstance();  
  469.             cd.setTime(date);  
  470.             mydate = cd.get(Calendar.DAY_OF_WEEK);  
  471.             // 獲取指定日期轉換成星期幾  
  472.         } catch (Exception e) {  
  473.             // TODO Auto-generated catch block  
  474.             e.printStackTrace();  
  475.         }  
  476.         if (mydate == 1) {  
  477.             week = ”星期日”;  
  478.         } else if (mydate == 2) {  
  479.             week = ”星期一”;  
  480.         } else if (mydate == 3) {  
  481.             week = ”星期二”;  
  482.         } else if (mydate == 4) {  
  483.             week = ”星期三”;  
  484.         } else if (mydate == 5) {  
  485.             week = ”星期四”;  
  486.         } else if (mydate == 6) {  
  487.             week = ”星期五”;  
  488.         } else if (mydate == 7) {  
  489.             week = ”星期六”;  
  490.         }  
  491.         return week;  
  492.     }  
  493. }  
package com.pts.peoplehui.utils;

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

public class DateUtils {

    public static String getTodayDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.getDefault());
        return format.format(new Date());
    }

    /**
     * 掉此方法輸入所要轉換的時間輸入例如("2014年06月14日16時09分00秒")返回時間戳
     * 
     * @param time
     * @return
     */
    public String data(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒",
                Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    public static String getTodayDateTimes() {
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日",
                Locale.getDefault());
        return format.format(new Date());
    }

    /**
     * 獲取當前時間
     * 
     * @return
     */
    public static String getCurrentTime_Today() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        return sdf.format(new java.util.Date());
    }

    /**
     * 調此方法輸入所要轉換的時間輸入例如("2014-06-14-16-09-00")返回時間戳
     * 
     * @param time
     * @return
     */
    public static String dataOne(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",
                Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    public static String getTimestamp(String time, String type) {
        SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    /**
     * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014年06月14日16時09分00秒")
     * 
     * @param time
     * @return
     */
    public static String times(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    /**
     * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014-06-14  16:09:00")
     * 
     * @param time
     * @return
     */
    public static String timedate(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    /**
     * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014年06月14日16:09")
     * 
     * @param time
     * @return
     */
    public static String timet(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    /**
     * @param time斜槓分開
     * @return
     */
    public static String timeslash(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd,HH:mm");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    /**
     * @param time斜槓分開
     * @return
     */
    public static String timeslashData(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
//      int i = Integer.parseInt(time);
        String times = sdr.format(new Date(lcc * 1000L));
        return times;

    }

    /**
     * @param time斜槓分開
     * @return
     */
    public static String timeMinute(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("HH:mm");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    public static String tim(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyyMMdd HH:mm");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    public static String time(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    // 調用此方法輸入所要轉換的時間戳例如(1402733340)輸出("2014年06月14日16時09分00秒")
    public static String times(long timeStamp) {
        SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日  #  HH:mm");
        return sdr.format(new Date(timeStamp)).replaceAll("#",
                getWeek(timeStamp));

    }

    private static String getWeek(long timeStamp) {
        int mydate = 0;
        String week = null;
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(timeStamp));
        mydate = cd.get(Calendar.DAY_OF_WEEK);
        // 獲取指定日期轉換成星期幾
        if (mydate == 1) {
            week = "週日";
        } else if (mydate == 2) {
            week = "週一";
        } else if (mydate == 3) {
            week = "週二";
        } else if (mydate == 4) {
            week = "週三";
        } else if (mydate == 5) {
            week = "週四";
        } else if (mydate == 6) {
            week = "週五";
        } else if (mydate == 7) {
            week = "週六";
        }
        return week;
    }

    // 並用分割符把時間分成時間數組
    /**
     * 調用此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014-06-14-16-09-00")
     * 
     * @param time
     * @return
     */
    public String timesOne(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    public static String timesTwo(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    }

    /**
     * 並用分割符把時間分成時間數組
     * 
     * @param time
     * @return
     */
    public static String[] timestamp(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        String[] fenge = times.split("[年月日時分秒]");
        return fenge;
    }

    /**
     * 根據傳遞的類型格式化時間
     * 
     * @param str
     * @param type
     *            例如:yy-MM-dd
     * @return
     */
    public static String getDateTimeByMillisecond(String str, String type) {

        Date date = new Date(Long.valueOf(str));

        SimpleDateFormat format = new SimpleDateFormat(type);

        String time = format.format(date);

        return time;
    }

    /**
     * 分割符把時間分成時間數組
     * 
     * @param time
     * @return
     */
    public String[] division(String time) {

        String[] fenge = time.split("[年月日時分秒]");

        return fenge;

    }

    /**
     * 輸入時間戳變星期
     * 
     * @param time
     * @return
     */
    public static String changeweek(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        Date date = null;
        int mydate = 0;
        String week = null;
        try {
            date = sdr.parse(times);
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            mydate = cd.get(Calendar.DAY_OF_WEEK);
            // 獲取指定日期轉換成星期幾
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (mydate == 1) {
            week = "星期日";
        } else if (mydate == 2) {
            week = "星期一";
        } else if (mydate == 3) {
            week = "星期二";
        } else if (mydate == 4) {
            week = "星期三";
        } else if (mydate == 5) {
            week = "星期四";
        } else if (mydate == 6) {
            week = "星期五";
        } else if (mydate == 7) {
            week = "星期六";
        }
        return week;

    }

    /**
     * 獲取日期和星期 例如:2014-11-13 11:00 星期一
     * 
     * @param time
     * @param type
     * @return
     */
    public static String getDateAndWeek(String time, String type) {
        return getDateTimeByMillisecond(time + "000", type) + "  "
                + changeweekOne(time);
    }

    /**
     * 輸入時間戳變星期
     * 
     * @param time
     * @return
     */
    public static String changeweekOne(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        Date date = null;
        int mydate = 0;
        String week = null;
        try {
            date = sdr.parse(times);
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            mydate = cd.get(Calendar.DAY_OF_WEEK);
            // 獲取指定日期轉換成星期幾
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (mydate == 1) {
            week = "星期日";
        } else if (mydate == 2) {
            week = "星期一";
        } else if (mydate == 3) {
            week = "星期二";
        } else if (mydate == 4) {
            week = "星期三";
        } else if (mydate == 5) {
            week = "星期四";
        } else if (mydate == 6) {
            week = "星期五";
        } else if (mydate == 7) {
            week = "星期六";
        }
        return week;

    }

    /**
     * 獲取當前時間
     * 
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(new java.util.Date());
    }

    /**
     * 輸入日期如(2014年06月14日16時09分00秒)返回(星期數)
     * 
     * @param time
     * @return
     */
    public String week(String time) {
        Date date = null;
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        int mydate = 0;
        String week = null;
        try {
            date = sdr.parse(time);
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            mydate = cd.get(Calendar.DAY_OF_WEEK);
            // 獲取指定日期轉換成星期幾
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (mydate == 1) {
            week = "星期日";
        } else if (mydate == 2) {
            week = "星期一";
        } else if (mydate == 3) {
            week = "星期二";
        } else if (mydate == 4) {
            week = "星期三";
        } else if (mydate == 5) {
            week = "星期四";
        } else if (mydate == 6) {
            week = "星期五";
        } else if (mydate == 7) {
            week = "星期六";
        }
        return week;
    }

    /**
     * 輸入日期如(2014-06-14-16-09-00)返回(星期數)
     * 
     * @param time
     * @return
     */
    public String weekOne(String time) {
        Date date = null;
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        int mydate = 0;
        String week = null;
        try {
            date = sdr.parse(time);
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            mydate = cd.get(Calendar.DAY_OF_WEEK);
            // 獲取指定日期轉換成星期幾
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (mydate == 1) {
            week = "星期日";
        } else if (mydate == 2) {
            week = "星期一";
        } else if (mydate == 3) {
            week = "星期二";
        } else if (mydate == 4) {
            week = "星期三";
        } else if (mydate == 5) {
            week = "星期四";
        } else if (mydate == 6) {
            week = "星期五";
        } else if (mydate == 7) {
            week = "星期六";
        }
        return week;
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章