java获取当前时间的方式【转】

  1 import java.sql.Timestamp;
  2 import java.text.ParsePosition;
  3 import java.text.SimpleDateFormat;
  4 import java.util.Date;
  5 
  6 import com.ttsoft.framework.util.DateUtil;
  7 
  8 /**
  9  * Title: 时间获取
 10  * Description: 当前时间
 11  * Copyright: Copyright   2010
 12  * Company: 
 13  * @author jiq
 14  * @version 1.0
 15  */
 16 public class XJPDateUtil extends DateUtil {
 17     public static final String[] months = { "一月", "二月", "三月", "四月", "五月", "六月",
 18      "七月", "八月", "九月", "十月", "十一月", "十二月", };
 19 
 20     public static final String[] quarters = { "一季度", "二季度", "三季度", "四季度" };
 21 
 22     public XJPDateUtil() {
 23     }
 24 
 25     /**
 26      * 获取日期字符串。
 27      * 
 28      * <pre>
 29      *  日期字符串格式: yyyyMMdd
 30      *  其中:
 31      *      yyyy   表示4位年。
 32      *      MM     表示2位月。
 33      *      dd     表示2位日。
 34      * </pre>
 35      * 
 36      * @return String "yyyyMMdd"格式的日期字符串。
 37      */
 38     public static String getDate() {
 39  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
 40 
 41  return formatter.format(new Date());
 42     }
 43 
 44     /**
 45      * 获取当前年度字符串。
 46      * 
 47      * <pre>
 48      *  日期字符串格式: yyyy
 49      *  其中:
 50      *      yyyy   表示4位年。
 51      * </pre>
 52      * 
 53      * @return String "yyyy"格式的当前年度字符串。
 54      */
 55     public static String getNowYear() {
 56  SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
 57 
 58  return formatter.format(new Date());
 59     }
 60 
 61     /**
 62      * 获取当前月度字符串。
 63      * 
 64      * <pre>
 65      *  日期字符串格式: MM
 66      *  其中:
 67      *      MM   表示4位年。
 68      * </pre>
 69      * 
 70      * @return String "yyyy"格式的当前月度字符串。
 71      */
 72     public static String getNowMonth() {
 73  SimpleDateFormat formatter = new SimpleDateFormat("MM");
 74 
 75  return formatter.format(new Date());
 76     }
 77 
 78     /**
 79      * 获取当前月度字符串。
 80      * 
 81      * <pre>
 82      *  日期字符串格式: dd
 83      *  其中:
 84      *      dd   表示4位年。
 85      * </pre>
 86      * 
 87      * @return String "yyyy"格式的当前月度字符串。
 88      */
 89     public static String getNowDay() {
 90  SimpleDateFormat formatter = new SimpleDateFormat("dd");
 91 
 92  return formatter.format(new Date());
 93     }
 94 
 95     /**
 96      * 获取日期字符串。
 97      * 
 98      * <pre>
 99      *  日期字符串格式: yyyyMMdd
100      *  其中:
101      *      yyyy   表示4位年。
102      *      MM     表示2位月。
103      *      dd     表示2位日。
104      * </pre>
105      * 
106      * @param date
107      *                需要转化的日期。
108      * @return String "yyyyMMdd"格式的日期字符串。
109      */
110     public static String getDate(Date date) {
111  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
112 
113  return formatter.format(date);
114     }
115 
116     /**
117      * 获取日期字符串。 
118      * 
119      * <pre>
120      *  日期字符串格式: yyyyMMdd
121      *  其中:
122      *      yyyy   表示4位年。
123      *      MM     表示2位月。
124      *      dd     表示2位日。
125      * </pre>
126      * 
127      * @param date
128      *                需要转化的日期。
129      * @return String "yyyyMMdd"格式的日期字符串。
130      */
131     /**
132      * 将指定的日期字符串转化为日期对象
133      * 
134      * @param dateStr
135      *                日期字符串
136      * @return java.util.Date
137      * @roseuid 3F39FE450385
138      */
139     public static Date getDate(String dateStr) {
140  if (XJPTypeChecker.isDate(dateStr)) { // 日期型
141      SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
142      try {
143   java.util.Date date = df.parse(dateStr);
144   return date;
145      } catch (Exception ex) {
146   Logger.write("日期格式不符合或者日期为空!请检查!");
147   return null;
148      } // end try - catch
149  } else if (XJPTypeChecker.isDatetime(dateStr)) { // 日期时间型
150      SimpleDateFormat df = new SimpleDateFormat(
151       "yyyy-MM-dd HH:mm:ss.SSS");
152      try {
153   java.util.Date date = df.parse(dateStr);
154   return date;
155      } catch (Exception ex) {
156   return null;
157      } // end try - catch
158  } // end if
159  return null;
160     }
161 
162     /**
163      * 获取日期字符串。
164      * 
165      * <pre>
166      *  日期字符串格式: yyyy-MM-dd
167      *  其中:
168      *      yyyy   表示4位年。
169      *      MM     表示2位月。
170      *      dd     表示2位日。
171      * </pre>
172      * 
173      * @return String "yyyy-MM-dd"格式的日期字符串。
174      */
175     public static String getHyphenDate() {
176  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
177 
178  return formatter.format(new Date());
179     }
180 
181     /**
182      * 获取日期字符串。
183      * 
184      * <pre>
185      *  日期字符串格式: yyyy-MM-dd
186      *  其中:
187      *      yyyy   表示4位年。
188      *      MM     表示2位月。
189      *      dd     表示2位日。
190      * </pre>
191      * 
192      * @param date
193      *                需要转化的日期。
194      * @return String "yyyy-MM-dd"格式的日期字符串。
195      */
196     public static String getHyphenDate(Date date) {
197  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
198 
199  return formatter.format(date);
200     }
201 
202     /**
203      * 将"yyyyMMdd"格式的日期字符串转换为日期对象。
204      * 
205      * @param source
206      *                日期字符串。
207      * @return Date 日期对象。
208      */
209     public static Date parsePlainDate(String source) {
210  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
211 
212  return sdf.parse(source, new ParsePosition(0));
213     }
214 
215     /**
216      * 将“yyyy-MM-dd”格式的日期字符串转换为日期对象。
217      * 
218      * @param source
219      *                日期字符串。
220      * @return Date 日期对象。
221      */
222     public static Date parseHyphenDate(String source) {
223  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
224 
225  return sdf.parse(source, new ParsePosition(0));
226     }
227 
228     /**
229      * 将指定格式的日期字符串转换为日期对象。
230      * 
231      * @param source
232      *                日期字符串。
233      * @param pattern
234      *                模式。
235      * @return Date 日期对象。
236      */
237     public static Date parseDate(String source, String pattern) {
238  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
239 
240  return sdf.parse(source, new ParsePosition(0));
241     }
242 
243     /**
244      * 将“yyyy-MM-dd”格式的日期字符串转换为“yyyyMMdd”格式的日期字符串。
245      * 
246      * @param source
247      *                日期字符串。
248      * @return String "yyyymmdd"格式的日期字符串。
249      */
250     public static String toPlainDate(String source) {
251  Date date = parseHyphenDate(source);
252  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
253 
254  return formatter.format(date);
255     }
256 
257     /**
258      * 将“yyyyMMdd”格式的日期字符串转换为“yyyy-MM-dd”格式的日期字符串。
259      * 
260      * @param source
261      *                日期字符串。
262      * @return String "yyyy-MM-dd"格式的日期字符串。
263      */
264     public static String toHyphenDate(String source) {
265  Date date = parsePlainDate(source);
266  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
267 
268  return formatter.format(date);
269     }
270 
271     /**
272      * 获取时间戳,将日期对象转换为时间戳类型。
273      * 
274      * @param date
275      *                日期对象
276      * @return Timestamp 时间戳
277      */
278     public static Timestamp getTimestamp(Date date) {
279  return new Timestamp(date.getTime());
280     }
281 
282     /**
283      * 获取时间戳,将当前日期转换为时间戳类型。
284      * 
285      * @return Timestamp 时间戳
286      */
287     public static Timestamp getTimestamp() {
288  return new Timestamp(new Date().getTime());
289     }
290 
291     /**
292      * 将“yyyyMMdd”格式的日期字符串转换为Timestamp类型的对象。
293      * 
294      * @param source
295      *                日期字符串
296      * @return Timestamp 时间戳
297      */
298     public static Timestamp parseTimestamp(String source) {
299  Date date = parsePlainDate(source);
300 
301  return getTimestamp(date);
302     }
303 
304     /**
305      * 获得年度周期 <br>
306      * Example:<br>
307      * XJPDateUtil.getPeriodYear("20040229" , -1);<br>
308      * XJPDateUtil.getPeriodYear("20040228" , -1);<br>
309      * XJPDateUtil.getPeriodYear("20020230" , 2);<br>
310      * 
311      * @param source
312      *                时间串
313      * @param yearPeriod
314      *                年度周期 -1代表本时间的上一年度,以次类推。
315      * @return String 时间。
316      */
317     public static String getPeriodYear(String source, int yearPeriod) {
318  int p = Integer.parseInt(source.substring(0, 4)) + yearPeriod;
319  String newYear = String.valueOf(p)
320   + source.substring(4, source.length());
321  Date date = parsePlainDate(newYear);
322  String s = getDate(date);
323  int ny = Integer.parseInt(s);
324  int sy = Integer.parseInt(newYear);
325 
326  while (ny > sy) {
327      sy--;
328      ny = Integer.parseInt(getDate(parsePlainDate(String.valueOf(sy))));
329  }
330 
331  return String.valueOf(sy);
332     }
333 
334     /**
335      * 获取当前日期和时间
336      * 
337      * @return String
338      */
339     public static String getCurrentDateStr() {
340  Date date = new Date();
341  String str = null;
342  SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
343  str = df.format(date);
344  return str;
345     }
346 
347     /**
348      * 日期相加
349      *
350      * @param day
351      *             天数
352      * @return 返回相加后的日期
353      */
354     public static String addDate(int day) {
355  java.util.Calendar c = java.util.Calendar.getInstance();
356 
357  c.setTimeInMillis(System.currentTimeMillis() + ((long) day) * 24 * 3600
358   * 1000);
359  SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
360  return df.format(c.getTime());
361     }
362 
363     /**
364      * 返回毫秒
365      * 
366      * @param date
367      *             日期
368      * @return 返回毫秒
369      */
370     public static long getMillis(java.util.Date date) {
371  java.util.Calendar c = java.util.Calendar.getInstance();
372  c.setTime(date);
373  return c.getTimeInMillis();
374     }
375     /**
376      * 获取当前日期和时间
377      * @param format 日期格式 例:yyyy-MM-dd hh:mm
378      * @return String
379      */
380     public static String getNowDate(String format) {
381  Date date = new Date();
382  String str = null;
383  SimpleDateFormat df = new SimpleDateFormat(format);
384  str = df.format(date);
385  return str;
386     }
387     /**
388      * 将strmon的日期减小一个月
389      * @param mon
390      * @return
391      */
392     public static String getReduceMonDate(String strmon) {
393  if (strmon != null && !strmon.equals("")) {
394      Date mon = parseHyphenDate(strmon);
395      mon.setMonth(mon.getMonth() - 1);
396      return getHyphenDate(mon);
397  } else {
398      return "";
399  }
400     }
401   public static String getTimeStr(String dateStr){
402  Date date=getDate(dateStr);
403  String str = null;
404  SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
405  str = df.format(date);
406  return str;
407     }
408   public static String getTimeStr(){
409       String str="";
410       SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
411   str = df.format(new Date());
412   return str;
413   }
414 }

转载于http://blog.csdn.net/thl331860203/article/details/5912435

原文地址:https://www.cnblogs.com/paomoopt/p/3737515.html