J2ME 技術(shù)的學(xué)習與實踐者

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


          網(wǎng)站: JavaEye  作者: iwinyeah  鏈接:http://iwinyeah.javaeye.com/blog/173704  發(fā)表時間: 2008年03月19日

          聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責任!

          我的一個日期處理類,解決了時區(qū)問題,給有需要的人。
          package util;
          
          /**
           * --------------------------------------------------
           * 日期轉(zhuǎn)換對象
           * --------------------------------------------------
           * 主要提供日期與1970-01-01后的天數(shù)的轉(zhuǎn)換和到字符串的轉(zhuǎn)換
           * --------------------------------------------------
           * 
           * @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; // 一天的微秒數(shù)
          
          	private static int rawOffset = TimeZone.getDefault().getRawOffset();
          
          	/**
          	 * 將日期轉(zhuǎn)換為1970-01-01后的天數(shù)
          	 * 
          	 * @param Date
          	 *            theDate 要計算天數(shù)的日期
          	 * @return int 所傳入日期與1970-01-01相差的天數(shù)
          	 */
          	public static int dateToDay(Date theDate) {
          		return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);
          	}
          
          	/**
          	 * 將1970-01-01后的天數(shù)轉(zhuǎn)換為日期
          	 * 
          	 * @param int
          	 *            要取的日期與1970-01-01相差的天數(shù)
          	 * @return Date theDate 與1970-01-01相差相應(yīng)天數(shù)的日期
          	 */
          	public static Date dayToDate(int day) {
          		return new Date(day * MSEC_EVERYDAY);
          	}
          
          	/**
          	 * 取今天與1970-01-01相差的天數(shù)
          	 * 
          	 * @return int 取今天與1970-01-01相差的天數(shù)
          	 */
          	public static int toDay() {
          		return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);
          	}
          
          	/**
          	 * 將日期轉(zhuǎn)換為年月日字符串
          	 * 
          	 * @param int
          	 *            theDay 與1970-01-01相差的天數(shù)
          	 * @return String 對應(yīng)日期年月日形式的字符串
          	 */
          	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);
          	}
          
          	/**
          	 * 將日期轉(zhuǎn)換為年月字符串
          	 * 
          	 * @param int
          	 *            theDay 與1970-01-01相差的天數(shù)
          	 * @return String 對應(yīng)日期年月形式的字符串
          	 */
          	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);
          	}
          
          	/**
          	 * 將日期轉(zhuǎn)換為月日字符串
          	 * 
          	 * @param int
          	 *            theDay 與1970-01-01相差的天數(shù)
          	 * @return String 對應(yīng)日期月日形式的字符串
          	 */
          	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);
          	}
          
          	/**
          	 * 將日期轉(zhuǎn)換為當月一號
          	 * 
          	 * @param int
          	 *            theDay 與1970-01-01相差的天數(shù)
          	 * @return int 對應(yīng)日期所在月份第一天與1970-01-01相差的天數(shù)
          	 */
          	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相差的天數(shù)
          	 * @return int 對應(yīng)日期所在年份
          	 */
          	public static int getYear(int theDay) {
          		_calendar.setTime(dayToDate(theDay));
          		return _calendar.get(Calendar.YEAR);
          	}
          
          	/**
          	 * 取日期所在月份
          	 * 
          	 * @param int
          	 *            theDay 與1970-01-01相差的天數(shù)
          	 * @return int 對應(yīng)日期所在月份
          	 */
          	public static int getMonth(int theDay) {
          		_calendar.setTime(dayToDate(theDay));
          		return _calendar.get(Calendar.MONTH);
          	}
          
          	/**
          	 * 取日期所在周次
          	 * 
          	 * @param int
          	 *            theDay 與1970-01-01相差的天數(shù)
          	 * @return int 對應(yīng)日期所在周次
          	 */
          	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 基礎(chǔ)知識

          主站蜘蛛池模板: 墨玉县| 宝清县| 建宁县| 承德市| 二连浩特市| 十堰市| 忻州市| 蓝山县| 玉门市| 富川| 青冈县| 和林格尔县| 衡山县| 子洲县| 西乌| 淳安县| 茌平县| 高唐县| 宁晋县| 财经| 宁乡县| 鹤岗市| 志丹县| 晋江市| 昆明市| 天台县| 吐鲁番市| 拜城县| 宝山区| 湘乡市| 米脂县| 昆山市| 思南县| 陕西省| 仪征市| 荆门市| 雷州市| 岳阳市| 淮阳县| 阿坝县| 沙坪坝区|