zoukankan      html  css  js  c++  java
  • 日期格式化和计算工具

    package com.input4hua.xxx.utils;
    
    import com.google.common.collect.Maps;
    import org.joda.time.DateTime;
    import org.joda.time.LocalDate;
    import org.joda.time.LocalTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    import java.util.Date;
    import java.util.Locale;
    import java.util.Map;
    
    /**
    * 时间格式转换工具类
    *
    * Created by jiahua.hu on 2017/12/18.
    */
    public class MyTimeFormatterUtils {
    
        public static final String TIME_FORMAT_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
        public static final String TIME_FORMAT_PATTERN_YEARMONTHDAY = "yyyy-MM-dd";
        public static final String TIME_FORMAT_PATTERN_HOURMINSEC = "HH:mm:ss";
        public static final String TIME_FORMAT_PATTERN_SHORTDATE_TIME = "yyyy/MM/dd HH:mm:ss";
        public static final String TIME_FORMAT_PATTERN_SHORTDATE = "yyyy/MM/dd";
        public static final String TIME_FORMAT_PATTERN_LONGDATE = "yyyy/MM/dd EE";
        private static final Map<Locale,String> TIME_FORMAT_PATTERN_LOCAL = Maps.newHashMap();
    
        static {
            TIME_FORMAT_PATTERN_LOCAL.put(Locale.CHINA,"yyyy年MM月dd日 EE");
        }
    
        /**
         * 字符转换时间,默认转换器
         * @param dateStr
         * @return
         */
        public static Date String2Date(String dateStr){
            return String2Date(dateStr,TIME_FORMAT_PATTERN_DEFAULT);
        }
    
        /**
         * 字符转换时间,指定自定义转换器
         * @param dateStr
         * @param formatPattern
         * @return
         */
        public static Date String2Date(String dateStr, String formatPattern){
            DateTimeFormatter format = DateTimeFormat.forPattern(formatPattern);
            DateTime dateTime = DateTime.parse(dateStr, format);
            return dateTime.toDate();
        }
    
        /**
         * 时间转换字符,默认转换器
         * @param date
         * @return
         */
        public static String Date2String(Date date){
            return Date2String(date,TIME_FORMAT_PATTERN_DEFAULT);
        }
    
        /**
         * 时间转换字符,指定自定义转换器
         * @param date
         * @param formatPattern
         * @return
         */
        public static String Date2String(Date date, String formatPattern){
            return new DateTime(date).toString(formatPattern);
        }
    
        /**
         * 当前时间
         * @return 时间格式
         */
        public static Date nowDate(){
            return DateTime.now().toDate();
        }
    
        /**
         * 当前年月日
         * @return 时间格式
         */
        public static Date nowYearMonthDayDate(){
            return LocalDate
                    .now()
                    .toDate();
        }
    
    
        /**
         * 当前时间
         * @return 字符
         */
        public static String nowStr(){
            return DateTime
                    .now()
                    .toString(TIME_FORMAT_PATTERN_DEFAULT);
        }
    
        /**
         * 当前年月日
         * @return 字符
         */
        public static String nowYearMonthDayStr(){
            return LocalDate
                    .now()
                    .toString(TIME_FORMAT_PATTERN_YEARMONTHDAY);
        }
    
        /**
         * 当前时分秒
         * @return 字符
         */
        public static String nowHourMinutesSecondsStr(){
            return LocalTime
                    .now()
                    .toString(TIME_FORMAT_PATTERN_HOURMINSEC);
        }
    
        /**
         * 当前短日期
         * @return 字符
         */
        public static String nowShortDate(){
            return formatShortDate(LocalDate.now().toDate());
        }
    
        /**
         * 指定时间短日期
         * @param date
         * @return 字符
         */
        public static String formatShortDate(Date date){
            return new LocalDate(date).toString(TIME_FORMAT_PATTERN_SHORTDATE);
        }
    
        /**
         * 当前长日期
         * @return 字符
         */
        public static String nowLongDate(){
            return formatLongDate(LocalDate.now().toDate());
        }
    
        /**
         * 指定时间长日期
         * @param date
         * @return 字符
         */
        public static String formatLongDate(Date date){
            return new LocalDate(date).toString(TIME_FORMAT_PATTERN_LONGDATE);
        }
    
        /**
         * 返回本地时间
         * @param date
         * @return
         */
        public static String stringLocalDate(Date date){
            return Date2String(date,TIME_FORMAT_PATTERN_LOCAL.get(Locale.getDefault()));
        }
    
    }
    package com.input4hua.xxx.utils;
    
    import org.joda.time.*;
    
    import java.util.Date;
    import java.util.Locale;
    
    /**
    *
    * 时间计算工具类
    *
    * Created by jiahua.hu on 2017/12/18.
    */
    public class MyTimeCalcUtils {
    
        /**
         * 明天的当前时间
         * @return
         */
        public static Date tomorrow(){
           return nextDays(1);
        }
    
        /**
         * 后i天
         * @param i
         * @return
         */
        public static Date nextDays(int i){
            DateTime dateTime = DateTime
                    .now()
                    .plusDays(i);
            return dateTime.toDate();
        }
    
        /**
         * 昨天的当前时间
         * @return
         */
        public static Date yesterday(){
            return prevDays(1);
        }
    
        /**
         * 前i天
         * @param i
         * @return
         */
        public static Date prevDays(int i){
            DateTime dateTime = DateTime
                    .now()
                    .minusDays(i);
            return dateTime.toDate();
        }
    
        /**
         * 今天的开始时刻
         * @return
         */
        public static Date todayBegin(){
            return DateTime
                    .now()
                    .withTimeAtStartOfDay().toDate();
        }
    
        /**
         * 今天的结束时刻
         * @return
         */
        public static Date todayEnd(){
            return DateTime
                    .now()
                    .millisOfDay()
                    .withMaximumValue()
                    .toDate();
        }
    
        /**
         * 当前周的第一天
         * @return
         */
        public static Date firstDayOfWeek(){
            LocalDate dateTime = LocalDate.now().dayOfWeek().withMinimumValue();
            return dateTime.toDate();
        }
    
        /**
         * 当前周的最后一天
         * @return
         */
        public static Date lastDayOfWeek(){
            LocalDate dateTime = LocalDate.now().dayOfWeek().withMaximumValue();
            return dateTime.toDate();
        }
    
        /**
         * 当月的第一天
         * @return
         */
        public static Date firstDayOfMonth(){
            LocalDate dateTime = LocalDate.now().dayOfMonth().withMinimumValue();
            return dateTime.toDate();
        }
    
        /**
         * 当月的最后一天
         * @return
         */
        public static Date lastDayOfMonth(){
            LocalDate dateTime = LocalDate.now().dayOfMonth().withMaximumValue();
            return dateTime.toDate();
        }
    
        /**
         * 当年的第一天
         * @return
         */
        public static Date firstDayOfYear(){
            LocalDate dateTime = LocalDate.now().dayOfYear().withMinimumValue();
            return dateTime.toDate();
        }
    
        /**
         * 当年的最后一天
         * @return
         */
        public static Date lastDayOfYear(){
            LocalDate dateTime = LocalDate.now().dayOfYear().withMaximumValue();
            return dateTime.toDate();
        }
    
        /**
         * 相差天数
         * @param begin
         * @param end
         * @return
         */
        public static long daysOfDiffer(Date begin, Date end){
            return new Duration(new DateTime(begin), new DateTime(end)).getStandardDays();
        }
    
        /**
         * 相差小时数
         * @param begin
         * @param end
         * @return
         */
        public static long hoursOfDiffer(Date begin, Date end){
            return new Duration(new DateTime(begin), new DateTime(end)).getStandardHours();
        }
    
        /**
         * 相差分钟数
         * @param begin
         * @param end
         * @return
         */
        public static long minutesOfDiffer(Date begin, Date end){
            return new Duration(new DateTime(begin), new DateTime(end)).getStandardMinutes();
        }
    
        /**
         * 相差秒数
         * @param begin
         * @param end
         * @return
         */
        public static long secondsOfDiffer(Date begin, Date end){
            return new Duration(new DateTime(begin), new DateTime(end)).getStandardSeconds();
        }
    
        /**
         * 指定日期是否在选定日期里
         * @param index
         * @param begin
         * @param end
         * @return
         */
        public static boolean indexOfDays(Date index, Date begin, Date end){
            return new Interval(new DateTime(begin),new DateTime(end)).contains(new DateTime(index));
        }
    
        /**
         * 指定时间月是否是闰月
         * @param date
         * @return
         */
        public static boolean leapOfMonth(Date date){
            return new DateTime(date).monthOfYear().isLeap();
        }
    
        /**
         * 指定时间年是否是闰年
         * @param date
         * @return
         */
        public static boolean leapOfYear(Date date){
            return new DateTime(date).year().isLeap();
        }
    }
  • 相关阅读:
    DBCC Page查看表和索引数据
    sp_configure
    学习
    OpenRowSet, OpenDataSource
    sp output
    SQL Server中使用CLR调用.NET方法
    SQL Server 2005五个有用的动态管理对象
    SQL显示执行语句信息
    接规则,每三个一组编一个号
    C# winform 与 flash as 的交互通讯
  • 原文地址:https://www.cnblogs.com/input4hua/p/8094018.html
Copyright © 2011-2022 走看看