浏览 2151 次
锁定老帖子 主题:日期处理类
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-02
package com.cmp.yiwu.util; import java.util.Calendar; import java.sql.Date; import java.text.*; import java.sql.Time; import java.sql.Timestamp; /** * <p>Copyright: Copyright (c) 2006</p> * <p>Company: Norteksoft</p> * @author * @version 1.0 */ public class DateUtil { public DateUtil() { } public static String getCurrentDate() { String currentDate = null; String yyyy = Long.toString(Calendar.getInstance().get(Calendar.YEAR)); String mm = Long.toString(Calendar.getInstance().get(Calendar.MONTH) + 1); if (mm.length() == 1) { mm = "0" + mm; } String dd = Long.toString(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); if (dd.length() == 1) { dd = "0" + dd; } currentDate = yyyy + "-" + mm + "-" + dd; return currentDate; } public static Date getNowDate() { return Date.valueOf(getCurrentDate()); } public static String getCurrentTime() { int h = Calendar.getInstance().get(Calendar.HOUR); String hour = Long.toString(h); if (h > 12) { hour = "上午"; h = h - 12; } else if (h < 12) { hour = "下午"; } else { hour = "中午"; } hour += Long.toString(h) + "点"; int m = Calendar.getInstance().get(Calendar.MINUTE); String minute = Long.toString(m) + "分"; int s = Calendar.getInstance().get(Calendar.SECOND); String second = Long.toString(s) + "秒"; String currentTime = hour + ":" + minute + ":" + second; return currentTime; } public static String getCurrentDateAndTime() { String date = getCurrentDate(); String time = getCurrentTime(); return date + " " + time; } private static String[][] timeZoneList = null; private static Object timeZoneLock = new Object(); public static final long SECOND = 1000; public static final long MINUTE = 60 * SECOND; public static final long HOUR = 60 * MINUTE; public static final long DAY = 24 * HOUR; public static final long WEEK = 7 * DAY; //由java.util.Date到java.sql.Date的类型转换 public static java.sql.Date getSqlDate(java.util.Date date) { return new java.sql.Date(date.getTime()); } //返回一个日期字符串在星期中的顺序 public static int getDateInWeek(String strDate) { DateFormat df = DateFormat.getDateInstance(); try { df.parse(strDate); java.util.Calendar c = df.getCalendar(); int day = c.get(c.DAY_OF_WEEK) - c.SUNDAY; return day; } catch (ParseException e) { return -1; } } //一个日期上加天数 public static String DateAdd(String startDate, int dd) { java.text.DateFormat df = java.text.DateFormat.getDateInstance(); java.util.Date date = new java.util.Date(); try { date = df.parse(startDate); } catch (Exception ex) { System.out.print(ex); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, dd); String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1); if (mm.length() == 1) { mm = "0" + mm; } String day = String.valueOf(calendar.get(Calendar.DATE)); if (day.length() == 1) { day = "0" + day; } String returnDate = String.valueOf(calendar.get(Calendar.YEAR)) + "-" + mm + "-" + day; return returnDate; } //一个日期上加dd个工作日 public static String DateAddWorkday(String startDate, int dd) { java.text.DateFormat df = java.text.DateFormat.getDateInstance(); java.util.Date date = new java.util.Date(); try { date = df.parse(startDate); } catch (Exception ex) { System.out.print(ex); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); while (dd > 0) { //Calendar.DAY_OF_WEEK返回的 1 是星期天 if ( (calendar.get(Calendar.DAY_OF_WEEK) == 1) || (calendar.get(Calendar.DAY_OF_WEEK) == 7)) { calendar.add(Calendar.DATE, 1); } else { calendar.add(Calendar.DATE, 1); dd = -1; } } String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1); String day = String.valueOf(calendar.get(Calendar.DATE)); if (mm.length() == 1) { mm = "0" + mm; } if (day.length() == 1) { day = "0" + day; } String returnDate = String.valueOf(calendar.get(Calendar.YEAR)) + "-" + mm + "-" + day; return returnDate; } //返回当前日期,类型为格式"yyyy-mm-dd"的字符串 public static String getDate() { java.text.SimpleDateFormat d = new java.text.SimpleDateFormat(); d.applyPattern("yyyy-MM-dd"); java.util.Date nowdate = new java.util.Date(); String str_date = d.format(nowdate); return str_date; } public static String DateToStr(java.util.Date date) { java.text.SimpleDateFormat d = new java.text.SimpleDateFormat(); d.applyPattern("yyyy-MM-dd"); String str_date = d.format(date); return str_date; } //返回指定年月的天数 public static int getMonthDayNum(int year, int month) { if (month == 2) { return year % 400 != 0 && (year % 4 != 0 || year % 100 == 0) ? 28 : 29; } String SmallMonth = ",4,6,9,11,"; return SmallMonth.indexOf(String.valueOf(String.valueOf( (new StringBuffer( ",")).append(String.valueOf(month)).append(",")))) < 0 ? 31 : 30; } //返回当前时间 public static String getTime() { java.util.Date date = new java.util.Date(); java.sql.Time time; time = new Time(date.getTime()); String strTime = time.toString(); return strTime; } public static int getYearMonthDate(String strDate, String style) { int year; int month; int day; int firstDash; int secondDash; if (strDate == null) { return 0; } firstDash = strDate.indexOf('-'); secondDash = strDate.indexOf('-', firstDash + 1); if ( (firstDash > 0) & (secondDash > 0) & (secondDash < strDate.length() - 1)) { year = Integer.parseInt(strDate.substring(0, firstDash)); month = Integer.parseInt(strDate.substring(firstDash + 1, secondDash)); day = Integer.parseInt(strDate.substring(secondDash + 1)); } else { return 0; } if (style.equalsIgnoreCase("Y")) { return year; } else if (style.equalsIgnoreCase("M")) { return month; } else if (style.equalsIgnoreCase("D")) { return day; } else { return 0; } } public static int getCurrentHour() { SimpleDateFormat f = new SimpleDateFormat("HH"); String hh = f.format(new Timestamp(System.currentTimeMillis())); return Integer.parseInt(hh); } //返回两日期差的天数 public static int DateDiff(java.sql.Date date1, java.sql.Date date2) { int i = (int) ( (date1.getTime() - date2.getTime()) / 3600 / 24 / 1000); return i; } public static java.sql.Timestamp getTimestamp() { try { java.text.SimpleDateFormat myFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); java.util.Calendar calendar = java.util.Calendar.getInstance(); String mystrdate = myFormat.format(calendar.getTime()); return java.sql.Timestamp.valueOf(mystrdate); } catch (Exception e) { return null; } } /*****************songwhung***************/ public static String getTimeString() { try { java.text.SimpleDateFormat myFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); java.util.Calendar calendar = java.util.Calendar.getInstance(); String mystrdate = myFormat.format(calendar.getTime()); return mystrdate; } catch (Exception e) { return null; } } /*****************songwhung***************/ public static String getCurrentHourMS() { SimpleDateFormat f = new SimpleDateFormat("HH:MM:SS"); String hh = f.format(new Timestamp(System.currentTimeMillis())); return hh; } public static java.sql.Timestamp getTimestamp(String datestr) { try { java.text.SimpleDateFormat myFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String mystrdate = myFormat.format(myFormat.parse(datestr)); return java.sql.Timestamp.valueOf(mystrdate); } catch (Exception e) { return null; } } public static java.sql.Timestamp getDate(String datestr) { datestr = datestr + " 00:00:00"; try { java.text.SimpleDateFormat myFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); java.util.Date date = myFormat.parse(datestr); String mystrdate = myFormat.format(date); return java.sql.Timestamp.valueOf(mystrdate); } catch (Exception e) { return null; } } public static java.util.Date StrToDate(String strDate) { int year; int month; int day; int firstDash; int secondDash; if (strDate == null) { return null; } firstDash = strDate.indexOf('-'); secondDash = strDate.indexOf('-', firstDash + 1); if ( (firstDash > 0) & (secondDash > 0) & (secondDash < strDate.length() - 1)) { year = Integer.parseInt(strDate.substring(0, firstDash)); month = Integer.parseInt(strDate.substring(firstDash + 1, secondDash)); day = Integer.parseInt(strDate.substring(secondDash + 1)); Calendar c = java.util.Calendar.getInstance(); c.set(year, month - 1, day); return c.getTime(); } return null; } public static String getDate(java.sql.Date datestr) { try { java.text.SimpleDateFormat myFormat = new SimpleDateFormat( "MM/dd/yyyy"); String mystrdate = myFormat.format(datestr); return mystrdate; } catch (Exception e) { e.printStackTrace(); return null; } } /********四舍五入************/ public static float mathRound(float num, int n) { float realValue = 0; int dd = 1; double tempnum = 0; for (int i = 0; i < n; i++) { dd *= 10; } tempnum = num * dd; tempnum = Math.round(tempnum); realValue = (float) tempnum / dd; return realValue; } public static String getTimeStamp() { SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss"); return format.format(new Timestamp(System.currentTimeMillis())); } /** * * @param style * @return */ public static String getCurrentTime(String style) { SimpleDateFormat format = new SimpleDateFormat(style); return format.format(new Timestamp(System.currentTimeMillis())); } public static void main(String[] argv) { DateUtil date = new DateUtil(); System.out.println( (new java.util.Date())); System.out.println(date.getSqlDate(new java.util.Date())); System.out.println(date.getDate()); System.out.println(date.getTime()); System.out.println(date.DateAdd("2004-10-10", 2)); System.out.println(date.getTimestamp().toString()); String date2 = date.getTimestamp().toString(); System.out.println(date.StrToDate("2006-02-07")); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |