[導入]日期處理類(忽略時間)
網站: 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 閱讀(220) 評論(0) 編輯 收藏 所屬分類: Java 基礎知識