One-Way
爱一人,攀一山,追一梦
  1 import java.text.ParseException;
  2 import java.text.SimpleDateFormat;
  3 import java.util.Calendar;
  4 import java.util.Date;
  5 
  6 /**
  7  * Created by Edward on 2016/6/30.
  8  */
  9 public class TimeUtil {
 10 
 11 
 12     /**
 13      * 将 1467341232351 转换为 指定格式 "yyyy-MM-dd HH:mm:ss.SSS"
 14      * @param time        "1467342217645"
 15      * @param pattern     "yyyy-MM-dd HH:mm:ss.SSS"
 16      * @return            2016-07-01 11:03:37.645
 17      */
 18 
 19     public static String parseLongTime2String(String time, String pattern){
 20         Calendar calendar = Calendar.getInstance();
 21         calendar.setTimeInMillis(Long.valueOf(time));
 22         return new SimpleDateFormat(pattern).format(calendar.getTime());
 23     }
 24 
 25 
 26     /**
 27      * 将指定格式的字符串转化为long
 28      * @param time   "2016-07-01 10:52:41.792"
 29      * @param pattern "yyyy-MM-dd HH:mm:ss.SSS"
 30      * @return         1467341561792
 31      */
 32     public static long parseString2LongTime(String time, String pattern)
 33     {
 34         Date date = null;
 35         try {
 36             date = new SimpleDateFormat(pattern).parse(time);
 37         } catch (ParseException e) {
 38             e.printStackTrace();
 39         }
 40         return date.getTime();
 41     }
 42 
 43     /**
 44      *
 45      * @param time     "2016-07-01 10:52:41.792"
 46      * @param pattern  "yyyy-MM-dd HH:mm:ss.SSS"
 47      * @param info     "YEAR"
 48      * @return          2016
 49      */
 50     public static int parseString2SpecialInfo(String time, String pattern, String info)
 51     {
 52         Date date = null;
 53         try {
 54             date = new SimpleDateFormat(pattern).parse(time);
 55             Calendar c = Calendar.getInstance();
 56             c.setTime(date);
 57             if(info.equals("YEAR"))
 58             {
 59                 return c.get(Calendar.YEAR);
 60             }
 61             else if(info.equals("MONTH"))
 62             {
 63                 return c.get(Calendar.MONTH)+1;
 64             }
 65             else if(info.equals("DAY"))
 66             {
 67                 return c.get(Calendar.DAY_OF_MONTH);
 68             }
 69             else if(info.equals("HOUR"))
 70             {
 71                 return c.get(Calendar.HOUR_OF_DAY);
 72             }
 73             else if(info.equals("MINUTE"))
 74             {
 75                 return c.get(Calendar.MINUTE);
 76             }
 77             else if(info.equals("SECOND"))
 78             {
 79                 return c.get(Calendar.SECOND);
 80             }
 81             else if(info.equals("MILLISECOND"))
 82             {
 83                 return c.get(Calendar.MILLISECOND);
 84             }
 85             else
 86             {
 87                 return -1;
 88             }
 89         } catch (ParseException e) {
 90             e.printStackTrace();
 91         }
 92         return -1;
 93     }
 94 
 95 
 96 
 97 
 98     public static void main(String[] args)
 99     {
100         Calendar calendar = Calendar.getInstance();
101 
102         System.out.println(calendar.getTimeInMillis());
103 
104         System.out.println(TimeUtil.parseLongTime2String(String.valueOf(calendar.getTimeInMillis()),"yyyy-MM-dd HH:mm:ss.SSS"));
105 
106         System.out.println(TimeUtil.parseString2LongTime("2016-07-01 10:52:41.792","yyyy-MM-dd HH:mm:ss.SSS"));
107 
108         int year = TimeUtil.parseString2SpecialInfo("2016-07-01 10:52:41.792","yyyy-MM-dd HH:mm:ss.SSS","YEAR");
109         int month = TimeUtil.parseString2SpecialInfo("2016-07-01 10:52:41.792","yyyy-MM-dd HH:mm:ss.SSS","MONTH");
110         int day = TimeUtil.parseString2SpecialInfo("2016-07-01 10:52:41.792","yyyy-MM-dd HH:mm:ss.SSS","DAY");
111 
112         System.out.println("Year:"+year+",Month:"+month+",Day:"+day);
113     }
114 
115 }

 

posted on 2016-07-01 11:18  单行道|  阅读(639)  评论(1编辑  收藏  举报