博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 日历组件java.util.Calendar以及时间戳 java.sql.Timestamp
阅读量:6475 次
发布时间:2019-06-23

本文共 12403 字,大约阅读时间需要 41 分钟。

几年前有写过该类的工具类,但没有放到博客上。当然,要找关于该日历的使用,网上一搜就大把。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();    }    }

 

转载于:https://www.cnblogs.com/weijunqiang/articles/3770463.html

你可能感兴趣的文章
web.xml配置session
查看>>
NBU备份oracle全备脚本注释
查看>>
Kickstart无人职守安装RHEL5
查看>>
关于Properties的用法的详细解释
查看>>
网络连接状态详解
查看>>
四格漫画《MUXing》——他们在干什么
查看>>
正则表达式收藏
查看>>
校园网维护(一)
查看>>
mysql 之flush操作
查看>>
gentoo中使用emerge更新安装软件是出现的问题及解决方法
查看>>
TCP/IP
查看>>
wdmWin10下遍历PCI配置空间
查看>>
移动通信技术的发展
查看>>
使用表格分割图片
查看>>
我的友情链接
查看>>
为CentOS虚拟机添加第二块网卡
查看>>
Linux防火墙iptables学习笔记
查看>>
Java常用类(一)Object
查看>>
激励着我前进
查看>>
我的友情链接
查看>>