Date类操作

本文介绍了一个Java类DateUtil,提供了将日期转换为天数、年月日字符串等功能,便于日期处理。

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

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
private static Calendar _calendar = Calendar.getInstance(); // 用于日期计算

private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒数

private static int rawOffset = TimeZone.getDefault().getRawOffset();

/**
* 将日期转换为1970-01-01后的天数
*
* @param Date
* theDate 要计算天数的日期
* @return int 所传入日期与1970-01-01相差的天数
*/
public static int dateToDay(Date theDate) {
return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);
}

/**
* 将1970-01-01后的天数转换为日期
*
* @param int
* 要取的日期与1970-01-01相差的天数
* @return Date theDate 与1970-01-01相差相应天数的日期
*/
public static Date dayToDate(int day) {
return new Date(day * MSEC_EVERYDAY);
}

/**
* 取今天与1970-01-01相差的天数
*
* @return int 取今天与1970-01-01相差的天数
*/
public static int toDay() {
return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);
}

/**
* 将日期转换为年月日字符串
*
* @param int
* theDay 与1970-01-01相差的天数
* @return String 对应日期年月日形式的字符串
*/
public static String getYMD(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR) + "_"
+ (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
+ (_calendar.get(Calendar.MONTH) + 1) + "_"
+ (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
+ _calendar.get(Calendar.DATE);
}

/**
* 将日期转换为年月字符串
*
* @param int
* theDay 与1970-01-01相差的天数
* @return String 对应日期年月形式的字符串
*/
public static String getYM(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR) + "/"
+ (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
+ (_calendar.get(Calendar.MONTH) + 1);
}

/**
* 将日期转换为月日字符串
*
* @param int
* theDay 与1970-01-01相差的天数
* @return String 对应日期月日形式的字符串
*/
public static String getMD(int theDay) {
_calendar.setTime(dayToDate(theDay));
return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
+ (_calendar.get(Calendar.MONTH) + 1) + "/"
+ (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
+ _calendar.get(Calendar.DATE);
}

/**
* 将日期转换为当月一号
*
* @param int
* theDay 与1970-01-01相差的天数
* @return int 对应日期所在月份第一天与1970-01-01相差的天数
*/
public static int getMonthFirstDay(int theDay) {
_calendar.setTime(dayToDate(theDay));
_calendar.set(Calendar.DAY_OF_MONTH, 1);
return (int) (dateToDay(_calendar.getTime()));
}

/**
* 取日期所在年份
*
* @param int
* theDay 与1970-01-01相差的天数
* @return int 对应日期所在年份
*/
public static int getYear(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR);
}

/**
* 取日期所在月份
*
* @param int
* theDay 与1970-01-01相差的天数
* @return int 对应日期所在月份
*/
public static int getMonth(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.MONTH);
}

/**
* 取日期所在周次
*
* @param int
* theDay 与1970-01-01相差的天数
* @return int 对应日期所在周次
*/
public static int getWeek(int theDay) {
// 1971-01-03是星期日,从该日开始计算周次
_calendar.setTime(dayToDate(theDay));
return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值