时间工具类

/*
 * 文 件 名:  DateTimeUtils.java
 * 版    权:  CMRI. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  Seven Zhang 
 * 修改时间:  Jun 28, 2014
* 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.nnct.trbs.portal.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.nnct.trbs.portal.common.GlobalConstant;
/**
 * 時間工具類
 * 
 * @author Seven Zhang
 * @version [版本号, Jun 28, 2014]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class TimeUtils {
    public static final int FIRST_MONTH_OF_YEAR = 1;
    public static final int LAST_MONTH_OF_YEAR = 12;
    /**
     * 将毫秒字符串转成时间格式字符串
     * 
     * @param millis
     * @param pattern
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String format(String millis, String pattern) {
        long millisecond = NumberUtils.toLong(millis, 0L);
        return DateFormatUtils.format(millisecond, pattern);
    }
    /**
     * whether the time with specified format is after now
     * 
     * @param dateTime
     * @param pattern
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean isAfterNow(String dateTime, String pattern) {
        DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
        DateTime dt = dtf.parseDateTime(dateTime);
        return dt.isAfterNow();
    }
    /**
     * 格式化当前时间日期
     * 
     * @param pattern
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String formatNow(String pattern) {
        long nowMillis = DateTime.now().getMillis();
        return DateFormatUtils.format(nowMillis, pattern);
    }
    /**
     * 获取当前时间的毫秒String值
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getNowMillisAsStr() {
        long nowMillis = DateTime.now().getMillis();
        return String.valueOf(nowMillis);
    }
    /**
     * 判断给予时间是否在本周 通过获取周一是否同一天来判断
     * 
     * @param millis
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean isInCurrentWeek(long millis) {
        Date thisMonday = DateTime.now().withDayOfWeek(1).toDate();
        Date givenMonday = new DateTime(millis).withDayOfWeek(1).toDate();
        return DateUtils.isSameDay(thisMonday, givenMonday);
    }
    /**
     * 判断一个时间字符串是否完整格式or仅日期格式
     * 
     * @param timestamp
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean isFullDateTime(String timestamp) {
        return StringUtils.contains(timestamp, " ") && StringUtils.contains(timestamp, ":");
    }
    /**
     * 获取某月(默认当前月)的第一天,精确至毫秒
     * 
     * @param year
     * @param month
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static DateTime getFirstDayOfMonth() {
        return DateTime.now().dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue();
    }
    /**
     * 获取某月(默认当前月)的第一天,精确至毫秒
     * 
     * @param year
     * @param month
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static DateTime getFirstDayOfMonth(int year, int month) {
        if (year <= 0 || month < 0 || month > 12) {
            return getFirstDayOfMonth();
        }
        else if (month == 0) {
            return DateTime.now().year().setCopy(year).monthOfYear().setCopy(FIRST_MONTH_OF_YEAR).dayOfMonth()
                    .withMinimumValue().millisOfDay().withMinimumValue();
        }
        else {
            return DateTime.now().year().setCopy(year).monthOfYear().setCopy(month).dayOfMonth().withMinimumValue()
                    .millisOfDay().withMinimumValue();
        }
    }
    /**
     * 获取某月(默认当前月)的最后一天,精确至毫秒
     * 
     * @param year
     * @param month
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static DateTime getLastDayOfMonth() {
        return DateTime.now().dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue();
    }
    /**
     * 获取某月(默认当前月)的最后一天,精确至毫秒
     * 
     * @param year
     * @param month
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static DateTime getLastDayOfMonth(int year, int month) {
        if (year <= 0 || month < 0 || month > 12) {
            return getLastDayOfMonth();
        }
        else if (month == 0) {
            return DateTime.now().year().setCopy(year).monthOfYear().setCopy(LAST_MONTH_OF_YEAR).dayOfMonth()
                    .withMaximumValue().millisOfDay().withMaximumValue();
        }
        else {
            return DateTime.now().year().setCopy(year).monthOfYear().setCopy(month).dayOfMonth().withMaximumValue()
                    .millisOfDay().withMaximumValue();
        }
    }
    /**
     * 将一个以秒为单位的时间段转化成(小时)分秒的数组
     * 
     * @param duration
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String[] parseDurationInArray(int duration) {
        String[] result = new String[] {};
        int hours = (int) Math.floor(duration / (60 * 60));
        if (hours > 0) {
            result = (String[]) ArrayUtils.add(result, String.valueOf(hours));
        }
        int minutes = (int) Math.floor((duration % (60 * 60)) / 60);
        result = (String[]) ArrayUtils.add(result, String.valueOf(minutes));
        int seconds = (int) Math.floor(duration % 60);
        result = (String[]) ArrayUtils.add(result, String.valueOf(seconds));
        return result;
    }
    /**
     * 获取今天0点毫秒
     * 
     * @return
     */
    public static long getMinMillisOfToday() {
        return DateTime.now().millisOfDay().withMinimumValue().getMillis();
    }
    /**
     * 获取今天24点毫秒
     * 
     * @return
     */
    public static long getMaxMillisOfToday() {
        return DateTime.now().millisOfDay().withMinimumValue().plusDays(1).getMillis();
    }
    /**
     * 将日期字符串转换为长整型
     * 
     * @param date
     *            日期字符串形式,格式:yyyy-MM-dd
     * @param format
     *            日期格式
     * @return long型日期
     */
    public static long convert2long(String date) {
        try {
            if (StringUtils.isNotBlank(date)) {
                SimpleDateFormat sf = new SimpleDateFormat(GlobalConstant.DATE_TIME_PATTERN);
                return sf.parse(date).getTime();
            }
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }
    /**
     * 将精确到秒的时间转换为长整型
     * 
     * @param user_time
     * @return
     */
    public static String getTime(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat(GlobalConstant.FULL_DATE_TIME_PARTERN);
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            re_time = String.valueOf(l);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return re_time;
    }
    /**
     * Check if two date periods overlap
     * 
     * @param startDate1
     * @param endDate1
     * @param startDate2
     * @param endDate2
     * @return
     */
    public static boolean isOverlapping(String startDate1, String endDate1, String startDate2, String endDate2) {
        DateTime startTime1 = new DateTime(startDate1);
        DateTime endTime1 = new DateTime(endDate1);
        DateTime startTime2 = new DateTime(startDate2);
        DateTime endTime2 = new DateTime(endDate2);
        Interval interval1 = new Interval(startTime1, endTime1);
        Interval interval2 = new Interval(startTime2, endTime2);
        return interval1.overlaps(interval2);
    }
    
    public static String getCurrentDay(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format.format(date);
        return dateString;
    }
}

转载于:https://my.oschina.net/loveryuan/blog/402921

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hutool是一个Java开源工具类库,其中包含了丰富的常用工具类,提供了一套规范的工具类,使得开发更加简化和高效。其中也包括了时间工具类。Hutool的时间工具类主要是针对日期和时间相关的操作提供了一些便捷的方法。你可以使用Hutool的DateUtil工具类来进行日期和时间的处理。 使用Hutool的DateUtil工具类,你可以进行以下操作: 1. 获取当前日期和时间; 2. 格式化日期和时间; 3. 解析字符串为日期和时间对象; 4. 比较两个日期或时间的大小; 5. 进行日期和时间的加减运算; 6. 设置日期和时间的指定部分(如年、月、日、小时、分钟等); 7. 获取日期和时间的指定部分(如年、月、日、小时、分钟等)。 通过引入Hutool的依赖,你可以在你的项目中使用Hutool的时间工具类。在pom.xml文件中,添加以下依赖: ```xml <dependencies> <dependency> *** </dependency> </dependencies> ``` 然后,你可以使用Hutool的DateUtil工具类来进行日期和时间的处理。具体的使用方法可以参考Hutool的官方文档和API参考。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [HuTool从入门到精通1-日期和文件工具类入门](https://blog.csdn.net/weixin_44480609/article/details/125330109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值