首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Date种操作

2012-08-30 
Date类操作import java.util.Calendarimport java.util.Dateimport java.util.TimeZonepublic class Da

Date类操作
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);  
    }  
 


热点排行