几年前有写过该类的工具类,但没有放到博客上。当然,要找关于该日历的使用,网上一搜就大把。but,我还是记录下,以后可以直接自己博客上拿来用。
日历:java.util.Calendar
import java.sql.Timestamp;import java.util.Calendar;public class TimeHandler { /** * 获得当前年份 * @return */ public static int getYear(){ return Calendar.getInstance().get(Calendar.YEAR); } /** * 计算指定日期为当年第几周 * @param year 指定的年份 * @param month 指定的月份 * @param day 指定的日 * @return 指定日期为当年的第几周 */ public static int caculateWeekOfYear(int year,int month,int day){ Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DATE, day); return c.get(Calendar.WEEK_OF_YEAR); } /** * 获取本月第一天是星期几 * @return */ public static int getWeekOfFirstDay(Calendar c){ Calendar calendar = c; calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.get(Calendar.DAY_OF_WEEK); } /** * 获取本月1号是该年的第几周 * @return */ public static int getMonthStartWeek(Calendar c){ Calendar calendar = c; calendar.set(Calendar.DATE, 1); return calendar.get(Calendar.WEEK_OF_YEAR); } /** * 获取当天是该年的第几周 */ public static int getCurrentWeekOfYear(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.WEEK_OF_YEAR); } /** * 获取上月的总天数 * @return */ public static int getLastMonthDays(Calendar c){ Calendar calendar = c; calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 获取指定月份的总天数 * @return */ public static int getCurrentMonthDays(int month){ Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, month - 1); return c.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 获取指定年份有多少周 * @param year * @return */ public static int getTotalWeekOfYear(int year){ Calendar c = Calendar.getInstance(); return c.getActualMaximum(Calendar.WEEK_OF_YEAR); } /** * 判断指定月份是否是当前月 * @param month * @return */ public static boolean isCurrentMonth(int month){ Calendar c = Calendar.getInstance(); return (c.get(Calendar.MONTH) == month - 1)?true:false; } /** * 计算指定的月份共有多少天 */ public static int getTotalDaysOfMonth(int year, int month){ Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month-1); c.set(Calendar.DATE, 1); /** * 计算这个月有多少天 */ return c.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 指定日期零点(一天的开始)来获取毫秒数 * @param year * @param month * @param day * @return */ public static long getStartTimeByDay(int year, int month, int day){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTimeInMillis(); } /** * 获取year对应的今天的零点时的毫秒数 * @param year * @return */ public static long getOtherYearInTodayMinTime(int year){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); long time = calendar.getTimeInMillis(); return time; } /** * 获取year对应的今天23:59:59(一天的结束)来获取毫秒数 * @param year * @return */ public static long getOtherYearInTodayMaxTime(int year){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTimeInMillis(); } /** * 根据时间获取对应的日历时间 * @param time * @return */ public static Calendar getCalendar(long time){ Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); return calendar; } /** * 根据指定日历查找年份 * @param calendar * @return */ public static int getYearByCalendar(Calendar calendar){ return calendar.get(Calendar.YEAR); } /** * 根据指定日历及年份修改日历年份后的对应的时间(毫秒) * @param calendar * @param year * @return */ public static long getTimeByCalendarNewYear(Calendar calendar, int year){ calendar.set(Calendar.YEAR, year); return calendar.getTimeInMillis(); } /** * 根据指定日历及增减年数修改日历年份后的对应的时间(毫秒) * @param calendar * @param v * @return */ public static long getTimeByCalendarAddYearsAfter(Calendar calendar,int v ){ calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + v); return calendar.getTimeInMillis(); } /** * * @param calendar * @return */ public static long getTimeByCalendar(Calendar calendar){ return calendar.getTimeInMillis(); } /** * 获取当前所在的月份 * @return */ public static int getCurrentMonth(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.MONTH) + 1; } /** * 指定日期23:59:59(一天的结束)来获取毫秒数 * @param year * @param month * @param day * @return */ public static long getEndTimeByDay(int year, int month, int day){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); long time = calendar.getTimeInMillis(); return time; } /** * 获取今天0点的毫秒数 * @return */ public static long getTime(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); long time = calendar.getTimeInMillis(); return time; } /** * 获取当前天为几号 * @return */ public static int getCurrentDay(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 获取当前日期:yyyy-mm-dd * @return */ public static String getDate(){ Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String date = "" + year + "-" + (month >= 10?month:"0"+month) + "-" + (day >= 10?day:"0"+day); return date; } /** * 获取指定日历下的日期:yyyy-mm-dd * @return */ public static String getDate(Calendar calendar){ int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String date = "" + year + "-" + (month >= 10?month:"0"+month) + "-" + (day >= 10?day:"0"+day); return date; } /** * 获取指定时间对应的日期:yyyy-mm-dd * @return */ public static String getDate(long time){ Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String date = "" + year + "-" + (month >= 10?month:"0"+month) + "-" + (day >= 10?day:"0"+day); return date; } }
时间戳: java.sql.Timestamp
import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.TimeZone;import java.util.Date;public class TimestampUtil { private static SimpleDateFormat tsdf = null ; //timestamp date formatter private static SimpleDateFormat dtdf = null ; //date time date formatter private static SimpleDateFormat tsdp = null ; //timestamp date parser private static SimpleDateFormat dtdp = null ; //date time date parser private static SimpleDateFormat tp_dt = null;//日期时间格式 yyyy-MM-dd HH:mm:ss private static void init(){ if( null == dtdp ){ // TimeZone utcTz= TimeZone.getTimeZone("GMT+0:00"); TimeZone cnTz = TimeZone.getTimeZone("GMT+8:00"); tsdf = new SimpleDateFormat("yyyyMMddHHmmss"); tsdf.setTimeZone(cnTz); dtdf = new SimpleDateFormat("yyyyMMdd"); dtdf.setTimeZone(cnTz); tsdp = new SimpleDateFormat("yyyyMMddHHmmss");// tsdp.setTimeZone(utcTz); tsdp.setTimeZone(cnTz); dtdp = new SimpleDateFormat("yyyyMMdd");// dtdp.setTimeZone(utcTz); dtdp.setTimeZone(cnTz); tp_dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); tp_dt.setTimeZone(cnTz); } } public static String toTimestamp(Timestamp ts) { init(); return tsdf.format(ts); } public static String toTimestamp(Date ts) { init(); return tsdf.format(ts); } public static String toDateStr(Timestamp ts) { init(); return dtdf.format(ts); } public static String toDateStr(Date ts) { init(); return dtdf.format(ts); } //String2Timestamp: public static Timestamp parse(String str){ init(); if( null == str || 8 > str.length()){ return null ; } if( 14 < str.length() ){ str = str.substring(0,14); } int size = str.length(); for( int i = 0 ;i< size;++i){ char ch = str.charAt(i); if( ch <'0' || ch >'9' ){ return null; } } Date ret = null ; if( 8 == str.length()){ try{ ret = dtdp.parse(str); } catch(Exception ex){ ret = null; } return new Timestamp( ret.getTime()); } if( 14 > size) { for( int i = size ;i<14; i++){ str += "0"; } } try{ ret = tsdp.parse(str); } catch(Exception ex){ ret = null; } return new Timestamp( ret.getTime()); } /** * 当前年月日-yyyyMMdd * @return */ public static String getTimeIsDate(){ init(); return dtdp.format(System.currentTimeMillis()); } /** * 当前年月日-yyyyMMdd HH:mm:ss * @return */ public static String getTimeInDateTime(){ init(); return tp_dt.format(System.currentTimeMillis()); } /** * 获取当前的时间戳yyyy-MM-dd HH:mm:ss * @return */ public static Timestamp getTimestampByCurrentTime(){ return new Timestamp(System.currentTimeMillis()); } /** * 根据time获取时间戳yyyy-MM-dd HH:mm:ss * @param time * @return */ public static Timestamp getTimestampByTimeMillis(long time){ return new Timestamp(time); } /** * 获取当前的时间yyyyMMddHHmmss * @return */ public static String getTimeInTime(){ return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())).toString(); } }