java时间格式转换

  1 package org.shineway.com;
  2 
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Calendar;
  6 import java.util.Date;
  7 import java.util.GregorianCalendar;
  8 
  9 /**
 10  * 各类时间类型的转换方式
 11  * @author mengzw
 12  * @since 3.0 2014-2-28
 13  */
 14 public class TimeUtil {
 15 
 16     public TimeUtil() {
 17     }
 18 
 19     /**
 20      * 将 Date 类型转换成String 
 21      * @param date 时间 Date 类型参数
 22      * @return 返回为  yyyy-MM-dd HH:mm:ss 格式String
 23      */
 24     public static String getTimeString(Date date) {
 25         String s = "yyyy'-'MM'-'dd' 'HH:mm:ss";
 26         SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
 27         return simpledateformat.format(date);
 28     }
 29 
 30     /**
 31      * 得到当前时间的时分秒
 32      * @return HH:mm:ss格式的字符串
 33      */
 34     public static String getOnlyCurrentTimeString() {
 35         String s = "HH:mm:ss";
 36         SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
 37         Calendar calendar = Calendar.getInstance();
 38         return simpledateformat.format(calendar.getTime());
 39     }
 40 
 41     /**
 42      * 将字符串日期转换成Date类型
 43      * @param s 类型格式:'yyyy-MM-dd' / 'yyyy-MM-dd HH:mm:ss'
 44      * @param s1 需要转换的  String类型 日期
 45      * @return  Date 类型日期
 46      */
 47     public static Date getString2Date(String s, String s1) {
 48         Date date = null;
 49         try {
 50             date = (new SimpleDateFormat(s1)).parse(s);
 51         } catch (Exception exception) {
 52             date = null;
 53         }
 54         return date;
 55     }
 56 
 57     /**
 58      * 将 Date 类型转换成指定格式
 59      * @param date 数据
 60      * @param s 时间显示格式
 61      * @return String日期字符串
 62      */
 63     public static String getFormartString(Date date, String s) {
 64         SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
 65         if (null != date)
 66             return simpledateformat.format(date);
 67         else
 68             return null;
 69     }
 70 
 71     /**
 72      * 将 Calendar 类型转换为指定格式
 73      * @param calendar 数据
 74      * @param s 时间显示格式
 75      * @return String日期字符串
 76      */
 77     public static String getFormartString(Calendar calendar, String s) {
 78         SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
 79         return simpledateformat.format(calendar.getTime());
 80     }
 81 
 82     /**
 83      * 将 Calendar 时间类型转换为String
 84      * @param calendar
 85      * @return 返回yyyy-MM-dd HH-mm-ss格式
 86      */
 87     public static String getTimeString(Calendar calendar) {
 88         String s = "yyyy'-'MM'-'dd' 'HH'-'mm'-'ss";
 89         SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
 90         return simpledateformat.format(calendar.getTime());
 91     }
 92 
 93     /**
 94      * 将Calendar 时间类型转换成String
 95      * @param calendar
 96       * @return 返回 yyyy-MM-dd格式
 97      */
 98     public static String getDateString(Calendar calendar) {
 99         String s = "yyyy'-'MM'-'dd";
100         SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
101         return simpledateformat.format(calendar.getTime());
102     }
103 
104     /**
105      * 将String 日期字符转换成Calendar类型
106      * @param s 时间类型数据
107      * @param s1 需要转换格式
108      * @return Calendar日期数据
109      */
110     public static Calendar getCalendar(String s, String s1) {
111         SimpleDateFormat simpledateformat = new SimpleDateFormat(s1);
112         Calendar calendar = Calendar.getInstance();
113         try {
114             calendar.setTime(simpledateformat.parse(s));
115         } catch (ParseException parseexception) {
116             return null;
117         }
118         return calendar;
119     }
120 
121     /**
122      * 输入日期自动判断格式返回日期
123      * @param s 日期字符
124      * @return Calendar类型日期
125      */
126     public static Calendar getCalendar(String s) {
127         int i = s.length();
128         switch (i) {
129         case 19: // '23'
130             return getCalendar(s, "yyyy'-'MM'-'dd' 'HH:mm:ss");
131 
132         case 10: // '
'
133             return getCalendar(s, "yyyy'-'MM'-'dd");
134         }
135         return null;
136     }
137 
138     /**
139      * 得到当前为第几个季度
140      * @return
141      */
142     public static String getCurrentSeason() {
143         String s = getFormartString(Calendar.getInstance(), "MM");
144         String s1 = "";
145         if (s.equals("01") || s.equals("02") || s.equals("03"))
146             s1 = "1";
147         else if (s.equals("04") || s.equals("05") || s.equals("06"))
148             s1 = "2";
149         else if (s.equals("07") || s.equals("08") || s.equals("09"))
150             s1 = "3";
151         else if (s.equals("10") || s.equals("11") || s.equals("12"))
152             s1 = "4";
153         return s1;
154     }
155 
156     public static int getWeekOfYear(Date date) {
157         GregorianCalendar gregoriancalendar = new GregorianCalendar();
158         gregoriancalendar.setFirstDayOfWeek(2);
159         gregoriancalendar.setMinimalDaysInFirstWeek(7);
160         gregoriancalendar.setTime(date);
161         return gregoriancalendar.get(3);
162     }
163 
164     /**
165      * 判断是否为闰年
166      * @param i 年份
167      * @return 是闰年为true
168      */
169     public static boolean isLeapYear(int i) {
170         boolean flag = false;
171         if (i % 4 == 0 && i % 100 != 0)
172             flag = true;
173         else if (i % 400 == 0)
174             flag = true;
175         else
176             flag = false;
177         return flag;
178     }
179 
180 }
原文地址:https://www.cnblogs.com/mengzw/p/3573349.html