java工具-Calender工具类

本文提供了一组实用的Java工具类方法,用于判断指定日期是否为今天、昨天或前天,以及判断时间是否为上午,并提供了从24小时制转换到12小时制的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Calender主要用于时间解析转换。
Calender接口相关中文介绍见:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

相关代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* Created on 2018/07/23
*/
public class TimeUtil {
private static final String TAG = “TimeUtil”;
private static final boolean DEBUG = false;
private static final int TODAY = 0;
private static final int YESTERDY = -1;
private static final int THE_DAY_BEFORE_YESDAY = -2;

/**
 * 是否为前天
 * @param day (yyyy-MM-dd)
 * @return true, false
 * @throws ParseException
 */
public static boolean isTheDayBeforeYesterday(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                - pre.get(Calendar.DAY_OF_YEAR);

        if (diffDay == THE_DAY_BEFORE_YESDAY) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为昨天
 * @param day (yyyy-MM-dd)
 * @return true, false
 * @throws ParseException
 */
public static boolean isYesterday(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                - pre.get(Calendar.DAY_OF_YEAR);

        if (diffDay == YESTERDY) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为今天
 * @param day(yyyy-MM-dd)
 * @return true, false
 * @throws ParseException
 */
public static boolean isToday(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                - pre.get(Calendar.DAY_OF_YEAR);

        if (diffDay == TODAY) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为今年
 * @param day(yyyy-MM-dd)
 * @return  true, false
 * @throws ParseException
 */
public static boolean isThisYear(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        return true;
    }
    return false;
}

/**
 * 是否是AM.
 * @param time(HH:mm:ss)
 * @return true, false
 * @throws ParseException
 */
public static boolean isAM(String time) throws ParseException {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse(time);
    cal.setTime(date);

    //0 : AM, 1: PM
    int apm = cal.get(Calendar.AM_PM);
    return (apm == 0);
}

/**
 * 获取12小时制的时间 hh:mm。
 * @param time 24小时制时间(HH:mm:ss)
 * @return 12小时制的时间(hh:mm)
 * @throws ParseException
 */
public static String get12HourSystemTime(String time) throws ParseException {
    System.out.println("get12HourSystemTime()");
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse(time);
    cal.setTime(date);

    if (cal.get(Calendar.MINUTE) == 0) {
        return new StringBuilder().append(cal.get(Calendar.HOUR))
                .append(":").append(cal.get(Calendar.MINUTE)).append("0").toString();
    } else {
        return new StringBuilder().append(cal.get(Calendar.HOUR))
                .append(":").append(cal.get(Calendar.MINUTE)).toString();
    }
}

}

package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值