J2ME 技術的學習與實踐者

          [導入]日期處理類(忽略時間)


          網站: 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 基礎知識

          主站蜘蛛池模板: 贡嘎县| 安岳县| 神池县| 阿瓦提县| 安远县| 天镇县| 彭州市| 栖霞市| 双峰县| 涿州市| 诸暨市| 邹平县| 东兰县| 九寨沟县| 泸西县| 贵阳市| 贡嘎县| 虞城县| 东兰县| 余干县| 三明市| 塘沽区| 容城县| 仁怀市| 云和县| 西青区| 邵阳市| 奈曼旗| 贵州省| 清水河县| 玉门市| 湘潭县| 师宗县| 化德县| 新田县| 资兴市| 剑川县| 吴旗县| 墨脱县| 绵竹市| 宾阳县|