java 导入日期类_[导入]日期处理类(忽略时间)

JavaEye

作者: iwinyeah

链接:http://iwinyeah.javaeye.com/blog/173704

发表时间: 2008年03月19日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

我的一个日期处理类,解决了时区问题,给有需要的人。

package util;

/**

* --------------------------------------------------

* 日期转换对象

* --------------------------------------------------

* 主要提供日期与1970-01-01后的天数的转换和到字符串的转换

* --------------------------------------------------

*

* @author iwinyeah 李永超

* @version 1.0.0

* */

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) % 100 + "/"

+ (_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);

}

}

JavaEye推荐

http://iwinyeah.javaeye.com/blog/173704

posted on 2008-03-19 12:32 iwinyeah 阅读(151) 评论(0)  编辑  收藏 所属分类: Java 基础知识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值