java学习

java学习

 

java时间程序

import java.util.*;
002import java.text.*;
003import java.util.Calendar;
004public class VeDate {
005 /**
006  * 获取现在时间
007  *
008  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>  返回时间类型 yyyy-MM-dd HH:mm:ss
009  */
010 public static Date getNowDate() {
011  Date currentTime = new Date();
012  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
013  String dateString = formatter.format(currentTime);
014  ParsePosition pos = new ParsePosition(8);
015  Date currentTime_2 = formatter.parse(dateString, pos);
016  return currentTime_2;
017 }
018 /**
019  * 获取现在时间
020  *
021  * @return返回短时间格式 yyyy-MM-dd
022  */
023 public static Date getNowDateShort() {
024  Date currentTime = new Date();
025  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
026  String dateString = formatter.format(currentTime);
027  ParsePosition pos = new ParsePosition(8);
028  Date currentTime_2 = formatter.parse(dateString, pos);
029  return currentTime_2;
030 }
031 /**
032  * 获取现在时间
033  *
034  * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
035  */
036 public static String getStringDate() {
037  Date currentTime = new Date();
038  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
039  String dateString = formatter.format(currentTime);
040  return dateString;
041 }
042 /**
043  * 获取现在时间
044  *
045  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>  返回短时间字符串格式yyyy-MM-dd
046  */
047 public static String getStringDateShort() {
048  Date currentTime = new Date();
049  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
050  String dateString = formatter.format(currentTime);
051  return dateString;
052 }
053 /**
054  * 获取时间 小时:分;秒 HH:mm:ss
055  *
056  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
057  */
058 public static String getTimeShort() {
059  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
060  Date currentTime = new Date();
061  String dateString = formatter.format(currentTime);
062  return dateString;
063 }
064 /**
065  * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
066  *
067  * @param strDate
068  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
069  */
070 public static Date strToDateLong(String strDate) {
071  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
072  ParsePosition pos = new ParsePosition(0);
073  Date strtodate = formatter.parse(strDate, pos);
074  return strtodate;
075 }
076 /**
077  * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
078  *
079  * @param dateDate
080  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
081  */
082 public static String dateToStrLong(java.util.Date dateDate) {
083  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
084  String dateString = formatter.format(dateDate);
085  return dateString;
086 }
087 /**
088  * 将短时间格式时间转换为字符串 yyyy-MM-dd
089  *
090  * @param dateDate
091  * @param k
092  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
093  */
094 public static String dateToStr(java.util.Date dateDate) {
095  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
096  String dateString = formatter.format(dateDate);
097  return dateString;
098 }
099 /**
100  * 将短时间格式字符串转换为时间 yyyy-MM-dd
101  *
102  * @param strDate
103  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
104  */
105 public static Date strToDate(String strDate) {
106  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
107  ParsePosition pos = new ParsePosition(0);
108  Date strtodate = formatter.parse(strDate, pos);
109  return strtodate;
110 }
111 /**
112  * 得到现在时间
113  *
114  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
115  */
116 public static Date getNow() {
117  Date currentTime = new Date();
118  return currentTime;
119 }
120 /**
121  * 提取一个月中的最后一天
122  *
123  * @param day
124  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
125  */
126 public static Date getLastDate(long day) {
127  Date date = new Date();
128  long date_3_hm = date.getTime() - 3600000 * 34 * day;
129  Date date_3_hm_date = new Date(date_3_hm);
130  return date_3_hm_date;
131 }
132 /**
133  * 得到现在时间
134  *
135  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>  字符串 yyyyMMdd HHmmss
136  */
137 public static String getStringToday() {
138  Date currentTime = new Date();
139  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
140  String dateString = formatter.format(currentTime);
141  return dateString;
142 }
143 /**
144  * 得到现在小时
145  */
146 public static String getHour() {
147  Date currentTime = new Date();
148  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
149  String dateString = formatter.format(currentTime);
150  String hour;
151  hour = dateString.substring(11, 13);
152  return hour;
153 }
154 /**
155  * 得到现在分钟
156  *
157  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
158  */
159 public static String getTime() {
160  Date currentTime = new Date();
161  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
162  String dateString = formatter.format(currentTime);
163  String min;
164  min = dateString.substring(14, 16);
165  return min;
166 }
167 /**
168  * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
169  *
170  * @param sformat
171  *            yyyyMMddhhmmss
172  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
173  */
174 public static String getUserDate(String sformat) {
175  Date currentTime = new Date();
176  SimpleDateFormat formatter = new SimpleDateFormat(sformat);
177  String dateString = formatter.format(currentTime);
178  return dateString;
179 }
180 /**
181  * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
182  */
183 public static String getTwoHour(String st1, String st2) {
184  String[] kk = null;
185  String[] jj = null;
186  kk = st1.split(":");
187  jj = st2.split(":");
188  if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
189   return "0";
190  else {
191   double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
192   double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
193   if ((y - u) > 0)
194    return y - u + "";
195   else
196    return "0";
197  }
198 }
199 /**
200  * 得到二个日期间的间隔天数
201  */
202 public static String getTwoDay(String sj1, String sj2) {
203  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
204  long day = 0;
205  try {
206   java.util.Date date = myFormatter.parse(sj1);
207   java.util.Date mydate = myFormatter.parse(sj2);
208   day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
209  } catch (Exception e) {
210   return "";
211  }
212  return day + "";
213 }
214 /**
215  * 时间前推或后推分钟,其中JJ表示分钟.
216  */
217 public static String getPreTime(String sj1, String jj) {
218  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
219  String mydate1 = "";
220  try {
221   Date date1 = format.parse(sj1);
222   long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
223   date1.setTime(Time * 1000);
224   mydate1 = format.format(date1);
225  } catch (Exception e) {
226  }
227  return mydate1;
228 }
229 /**
230  * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
231  */
232 public static String getNextDay(String nowdate, String delay) {
233  try{
234  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
235  String mdate = "";
236  Date d = strToDate(nowdate);
237  long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
238  d.setTime(myTime * 1000);
239  mdate = format.format(d);
240  return mdate;
241  }catch(Exception e){
242   return "";
243  }
244 }
245 /**
246  * 判断是否润年
247  *
248  * @param ddate
249  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
250  */
251 public static boolean isLeapYear(String ddate) {
252  /**
253   * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
254   * 3.能被4整除同时能被100整除则不是闰年
255   */
256  Date d = strToDate(ddate);
257  GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
258  gc.setTime(d);
259  int year = gc.get(Calendar.YEAR);
260  if ((year % 400) == 0)
261   return true;
262  else if ((year % 4) == 0) {
263   if ((year % 100) == 0)
264    return false;
265   else
266    return true;
267  } else
268   return false;
269 }
270 /**
271  * 返回美国时间格式 26 Apr 2006
272  *
273  * @param str
274  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
275  */
276 public static String getEDate(String str) {
277  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
278  ParsePosition pos = new ParsePosition(0);
279  Date strtodate = formatter.parse(str, pos);
280  String j = strtodate.toString();
281  String[] k = j.split(" ");
282  return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
283 }
284 /**
285  * 获取一个月的最后一天
286  *
287  * @param dat
288  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
289  */
290 public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
291  String str = dat.substring(0, 8);
292  String month = dat.substring(5, 7);
293  int mon = Integer.parseInt(month);
294  if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
295   str += "31";
296  } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
297   str += "30";
298  } else {
299   if (isLeapYear(dat)) {
300    str += "29";
301   } else {
302    str += "28";
303   }
304  }
305  return str;
306 }
307 /**
308  * 判断二个时间是否在同一个周
309  *
310  * @param date1
311  * @param date2
312  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
313  */
314 public static boolean isSameWeekDates(Date date1, Date date2) {
315  Calendar cal1 = Calendar.getInstance();
316  Calendar cal2 = Calendar.getInstance();
317  cal1.setTime(date1);
318  cal2.setTime(date2);
319  int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
320  if (0 == subYear) {
321   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
322    return true;
323  } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
324   // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
325   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
326    return true;
327  } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
328   if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
329    return true;
330  }
331  return false;
332 }
333 /**
334  * 产生周序列,即得到当前时间所在的年度是第几周
335  *
336  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
337  */
338 public static String getSeqWeek() {
339  Calendar c = Calendar.getInstance(Locale.CHINA);
340  String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
341  if (week.length() == 1)
342   week = "0" + week;
343  String year = Integer.toString(c.get(Calendar.YEAR));
344  return year + week;
345 }
346 /**
347  * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
348  *
349  * @param sdate
350  * @param num
351  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
352  */
353 public static String getWeek(String sdate, String num) {
354  // 再转换为时间
355  Date dd = VeDate.strToDate(sdate);
356  Calendar c = Calendar.getInstance();
357  c.setTime(dd);
358  if (num.equals("1")) // 返回星期一所在的日期
359   c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
360  else if (num.equals("2")) // 返回星期二所在的日期
361   c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
362  else if (num.equals("3")) // 返回星期三所在的日期
363   c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
364  else if (num.equals("4")) // 返回星期四所在的日期
365   c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
366  else if (num.equals("5")) // 返回星期五所在的日期
367   c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
368  else if (num.equals("6")) // 返回星期六所在的日期
369   c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
370  else if (num.equals("0")) // 返回星期日所在的日期
371   c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
372  return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
373 }
374 /**
375  * 根据一个日期,返回是星期几的字符串
376  *
377  * @param sdate
378  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
379  */
380 public static String getWeek(String sdate) {
381  // 再转换为时间
382  Date date = VeDate.strToDate(sdate);
383  Calendar c = Calendar.getInstance();
384  c.setTime(date);
385  // int hour=c.get(Calendar.DAY_OF_WEEK);
386  // hour中存的就是星期几了,其范围 1~7
387  // 1=星期日 7=星期六,其他类推
388  return new SimpleDateFormat("EEEE").format(c.getTime());
389 }
390 public static String getWeekStr(String sdate){
391  String str = "";
392  str = VeDate.getWeek(sdate);
393  if("1".equals(str)){
394   str = "星期日";
395  }else if("2".equals(str)){
396   str = "星期一";
397  }else if("3".equals(str)){
398   str = "星期二";
399  }else if("4".equals(str)){
400   str = "星期三";
401  }else if("5".equals(str)){
402   str = "星期四";
403  }else if("6".equals(str)){
404   str = "星期五";
405  }else if("7".equals(str)){
406   str = "星期六";
407  }
408  return str;
409 }
410 /**
411  * 两个时间之间的天数
412  *
413  * @param date1
414  * @param date2
415  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
416  */
417 public static long getDays(String date1, String date2) {
418  if (date1 == null || date1.equals(""))
419   return 0;
420  if (date2 == null || date2.equals(""))
421   return 0;
422  // 转换为标准时间
423  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
424  java.util.Date date = null;
425  java.util.Date mydate = null;
426  try {
427   date = myFormatter.parse(date1);
428   mydate = myFormatter.parse(date2);
429  } catch (Exception e) {
430  }
431  long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
432  return day;
433 }
434 /**
435  * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
436  * 此函数返回该日历第一行星期日所在的日期
437  *
438  * @param sdate
439  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
440  */
441 public static String getNowMonth(String sdate) {
442  // 取该时间所在月的一号
443  sdate = sdate.substring(0, 8) + "01";
444  // 得到这个月的1号是星期几
445  Date date = VeDate.strToDate(sdate);
446  Calendar c = Calendar.getInstance();
447  c.setTime(date);
448  int u = c.get(Calendar.DAY_OF_WEEK);
449  String newday = VeDate.getNextDay(sdate, (1 - u) + "");
450  return newday;
451 }
452 /**
453  * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
454  *
455  * @param k
456  *            表示是取几位随机数,可以自己定
457  */
458 public static String getNo(int k) {
459  return getUserDate("yyyyMMddhhmmss") + getRandom(k);
460 }
461 /**
462  * 返回一个随机数
463  *
464  * @param i
465  * <a href="http://my.oschina.net/u/556800" target="_blank" rel="nofollow">@return</a>
466  */
467 public static String getRandom(int i) {
468  Random jjj = new Random();
469  // int suiJiShu = jjj.nextInt(9);
470  if (i == 0)
471   return "";
472  String jj = "";
473  for (int k = 0; k < i; k++) {
474   jj = jj + jjj.nextInt(9);
475  }
476  return jj;
477 }
478 /**
479  *
480  * @param args
481  */
482 public static boolean RightDate(String date) {
483  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
484  ;
485  if (date == null)
486   return false;
487  if (date.length() > 10) {
488   sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
489  } else {
490   sdf = new SimpleDateFormat("yyyy-MM-dd");
491  }
492  try {
493   sdf.parse(date);
494  } catch (ParseException pe) {
495   return false;
496  }
497  return true;
498 }
499 /***************************************************************************
500  * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
501  * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
502  **************************************************************************/
503 public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
504  Date currentTime = new Date();
505  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
506  String dateString = formatter.format(currentTime);
507  String s_nd = dateString.substring(0, 4); // 年份
508  String s_yf = dateString.substring(5, 7); // 月份
509  String s_rq = dateString.substring(8, 10); // 日期
510  String sreturn = "";
511  roc.util.MyChar mc = new roc.util.MyChar();
512  if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
513   if (nd.equals("1")) {
514    sreturn = s_nd;
515    // 处理间隔符
516    if (format.equals("1"))
517     sreturn = sreturn + "年";
518    else if (format.equals("2"))
519     sreturn = sreturn + "-";
520    else if (format.equals("3"))
521     sreturn = sreturn + "/";
522    else if (format.equals("5"))
523     sreturn = sreturn + ".";
524   }
525   // 处理月份
526   if (yf.equals("1")) {
527    sreturn = sreturn + s_yf;
528    if (format.equals("1"))
529     sreturn = sreturn + "月";
530    else if (format.equals("2"))
531     sreturn = sreturn + "-";
532    else if (format.equals("3"))
533     sreturn = sreturn + "/";
534    else if (format.equals("5"))
535     sreturn = sreturn + ".";
536   }
537   // 处理日期
538   if (rq.equals("1")) {
539    sreturn = sreturn + s_rq;
540    if (format.equals("1"))
541     sreturn = sreturn + "日";
542   }
543  } else {
544   // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
545   sdate = roc.util.RocDate.getOKDate(sdate);
546   s_nd = sdate.substring(0, 4); // 年份
547   s_yf = sdate.substring(5, 7); // 月份
548   s_rq = sdate.substring(8, 10); // 日期
549   if (nd.equals("1")) {
550    sreturn = s_nd;
551    // 处理间隔符
552    if (format.equals("1"))
553     sreturn = sreturn + "年";
554    else if (format.equals("2"))
555     sreturn = sreturn + "-";
556    else if (format.equals("3"))
557     sreturn = sreturn + "/";
558    else if (format.equals("5"))
559     sreturn = sreturn + ".";
560   }
561   // 处理月份
562   if (yf.equals("1")) {
563    sreturn = sreturn + s_yf;
564    if (format.equals("1"))
565     sreturn = sreturn + "月";
566    else if (format.equals("2"))
567     sreturn = sreturn + "-";
568    else if (format.equals("3"))
569     sreturn = sreturn + "/";
570    else if (format.equals("5"))
571     sreturn = sreturn + ".";
572   }
573   // 处理日期
574   if (rq.equals("1")) {
575    sreturn = sreturn + s_rq;
576    if (format.equals("1"))
577     sreturn = sreturn + "日";
578   }
579  }
580  return sreturn;
581 }
582 public static String getNextMonthDay(String sdate, int m) {
583  sdate = getOKDate(sdate);
584  int year = Integer.parseInt(sdate.substring(0, 4));
585  int month = Integer.parseInt(sdate.substring(5, 7));
586  month = month + m;
587  if (month < 0) {
588   month = month + 12;
589   year = year - 1;
590  } else if (month > 12) {
591   month = month - 12;
592   year = year + 1;
593  }
594  String smonth = "";
595  if (month < 10)
596   smonth = "0" + month;
597  else
598   smonth = "" + month;
599  return year + "-" + smonth + "-10";
600 }
601 public static String getOKDate(String sdate) {
602  if (sdate == null || sdate.equals(""))
603   return getStringDateShort();
604  if (!VeStr.Isdate(sdate)) {
605   sdate = getStringDateShort();
606  }
607  // 将“/”转换为“-”
608  sdate = VeStr.Replace(sdate, "/", "-");
609  // 如果只有8位长度,则要进行转换
610  if (sdate.length() == 8)
611   sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
612  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
613  ParsePosition pos = new ParsePosition(0);
614  Date strtodate = formatter.parse(sdate, pos);
615  String dateString = formatter.format(strtodate);
616  return dateString;
617 }
618 public static void main(String[] args) throws Exception {
619  try {
620   //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
621  } catch (Exception e) {
622   throw new Exception();
623  }
624  //System.out.println("sss");
625 }
626}

posted on 2013-01-18 15:29 杨军威 阅读(249) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜