1. 程式人生 > >別人那搞過來date工具mark一下

別人那搞過來date工具mark一下

package com.example.datepicker; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class MYdate extends Date { private static DateFormat dateTimeF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"
); public static String getDateTimeString(Date date) { if (date == null) return ""; return dateTimeF.format(date); } public static Date getDateFromDateTimeString(String dateTimeString) { try { return dateTimeF.parse(dateTimeString); } catch (ParseException e) { return null;
} } public static DateFormat dateShortTimeF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public static String getDateShortTimeString(Date date) { if (date == null) return ""; return dateShortTimeF.format(date); } public static Date getDateFromDateShortTimeString(String dateShortTimeString) { try
{ return dateShortTimeF.parse(dateShortTimeString); } catch (ParseException e) { return null; } } public static DateFormat dateE = new SimpleDateFormat("yyyy-MM"); public static String getYearAndMonthString(Date date) { if (date == null) return ""; return dateE.format(date); } public static DateFormat dateF = new SimpleDateFormat("yyyy-MM-dd"); public static String getDateString(Date date) { if (date == null) return ""; return dateF.format(date); } public static Date getDateFromDateString(String dateString) { try { return dateF.parse(dateString); } catch (ParseException e) { return null; } } private static DateFormat shortDateTimeF = new SimpleDateFormat("MM-dd HH:mm"); public static String getShortDateTimeString(Date date) { if (date == null) return ""; return shortDateTimeF.format(date); } private static DateFormat shortDateF = new SimpleDateFormat("MM-dd"); public static String getShortDateString(Date date) { if (date == null) return ""; return shortDateF.format(date); } public static DateFormat shortTimeF = new SimpleDateFormat("HH:mm"); public static String getShortTimeString(Date date) { if (date == null) return ""; return shortTimeF.format(date); } private static Calendar calendar = Calendar.getInstance(); public static Date addDayFromDate(Date date, int day) { calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, day); return calendar.getTime(); } //返回大寫日期 public static String getUpperDateTimeString(Date date) { calendar.setTime(date); return LYDate.toUpperNumber(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))) + "時" + LYDate.toUpperNumber(String.valueOf(calendar.get(Calendar.MINUTE))) + "分"; } public static String toUpperNumber(String dateString) { // if (CommonHelper.isNullOrBlank(dateString)) { // return ""; // } if (dateString.length() == 1) { dateString = "0" + dateString; } String string = null; String firNumStr = dateString.substring(0, 1); String secNumStr = dateString.substring(1, 2); int firNum = Integer.valueOf(firNumStr); int secNum = Integer.valueOf(secNumStr); String[] numArray = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}; if (firNum == 0) { string = numArray[secNum]; } else if (firNum == 1) { if (secNum == 0) { string = numArray[10]; } else { string = numArray[10] + numArray[secNum]; } } else { if (secNum == 0) { string = numArray[firNum] + numArray[10]; } else { string = numArray[firNum] + numArray[10] + numArray[secNum]; } } return string; } /** * 當月第一天 * * @return */ public static String getFirstDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); Date theDate = calendar.getTime(); GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DAY_OF_MONTH, 1); String day_first = dateF.format(gcLast.getTime()); StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00"); return str.toString(); } /** * 當月最後一天 * * @return */ public static String getLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DATE, -1); Date theDate = calendar.getTime(); String s = dateF.format(theDate); StringBuffer str = new StringBuffer().append(s).append(" 23:59:59"); return str.toString(); } //加天數 public static Date addDay(Date date, int day) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, day); Date theDate = calendar.getTime(); return theDate; } public static Date addMinute(String time, int minute) { Date date = getDateFromDateTimeString(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, (0 - minute)); Date theDate = calendar.getTime(); return theDate; } }