java設置時間偏移天數,

1,java

package com.banksteel.openerp.common.vo;

import java.io.Serializable;

public class TimeIntervalVO implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1568237892596588268L;
	private Long beginInMillis; // 設置時間當天的零點
	private Long endInMillis;  // 設置時間當天最後一秒
	private String beginInString; // 顯示時間
	private String endInString; // 顯示時間

	public Long getBeginInMillis() {
		return beginInMillis;
	}

	public void setBeginInMillis(Long beginInMillis) {
		this.beginInMillis = beginInMillis;
	}

	public Long getEndInMillis() {
		return endInMillis;
	}

	public void setEndInMillis(Long endInMillis) {
		this.endInMillis = endInMillis;
	}

	public String getBeginInString() {
		return beginInString;
	}

	public void setBeginInString(String beginInString) {
		this.beginInString = beginInString;
	}

	public String getEndInString() {
		return endInString;
	}

	public void setEndInString(String endInString) {
		this.endInString = endInString;
	}

	@Override
	public String toString() {
		return "TimeIntervalVO [beginInMillis=" + beginInMillis + ", endInMillis=" + endInMillis + ", beginInString="
				+ beginInString + ", endInString=" + endInString + "]";
	}

}

  2.web

/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

package cn.mysteel.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;

public class DateUtils
{

    public DateUtils()
    {
    }

    public static Date getDate(String s)
    {
        return getDate(s, null);
    }

    public static Date getJustDate(String s)
    {
        return getDate(s, "yyyy-MM-dd");
    }

    public static Date getDate(long date)
    {
        return getDate(date, null);
    }

    public static Date getJustDate(long date)
    {
        return getDate(date, "yyyy-MM-dd");
    }

    public static Date getDate(long date, String format)
    {
        if(StringUtils.isEmpty(format))
            format = "yyyy-MM-dd HH:mm:ss";
        return getDate(formatDate(new Date(date), format), format);
    }

    public static Date getDate(String s, String format)
    {
        Date date;
        try
        {
            if(StringUtils.isEmpty(format))
                format = "yyyy-MM-dd HH:mm:ss";
            date = (new SimpleDateFormat(format)).parse(s);
        }
        catch(Exception e)
        {
            date = new Date(0L);
        }
        return date;
    }

    public static String formatDate(long date, String format)
    {
        return formatDate(new Date(date), format);
    }

    public static String formatDate(long date)
    {
        return formatDate(new Date(date), null);
    }

    public static String formatJustDate(long date)
    {
        return formatDate(new Date(date), "yyyy-MM-dd");
    }

    public static String formatDate(Date date, String format)
    {
        if(StringUtils.isEmpty(format))
            format = "yyyy-MM-dd HH:mm:ss";
        return (new SimpleDateFormat(format)).format(date);
    }

    public static final String DATE_FROMAT1 = "yyyy-MM-dd";
    public static final String DATE_FROMAT2 = "yyyy-MM-dd HH:mm:ss";
}


/*
	DECOMPILATION REPORT

	Decompiled from: D:\eclipse\demo-maven\repository\cn\mysteel\shgl-core\1.0.0-SNAPSHOT\shgl-core-1.0.0-SNAPSHOT.jar
	Total time: 321 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/

  3apache

package com.banksteel.openerp.utils;

import java.util.Calendar;

import com.banksteel.openerp.common.vo.TimeIntervalVO;

import cn.mysteel.util.DateUtils;

/**
 * 時間工具類
 * 
 * @description:
 * @projectName:openerp-webapp
 * @className:TimeUtils.java
 * @createTime:2016年9月27日 下午4:02:40
 * @version 1.0
 */
public class TimeUtils {

	private TimeUtils() {
	};

	/**
	 * 獲取基準時間戳計算指定天數後的當天00:00:00.000至23:59:59.999時間戳區間
	 * 
	 * @param millis
	 *            基準時間戳
	 * @param dayOffset
	 *            偏移天數,0表示當天,正數表示基準時間戳以後N天,負數反之
	 * @createTime:2016年9月29日 下午5:19:40
	 */
	public static TimeIntervalVO getMillisInterval(Long millis, int dayOffset) {
		TimeIntervalVO interval = new TimeIntervalVO();
		Calendar calendar = Calendar.getInstance();
		if (millis > 0) {
			calendar.setTime(DateUtils.getDate(millis, "yyyy-MM-dd"));
			calendar.add(Calendar.DATE, dayOffset);
			interval.setBeginInMillis(calendar.getTimeInMillis());
			interval.setBeginInString(DateUtils.formatDate(calendar.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss.SSS"));
			interval.setEndInMillis(calendar.getTimeInMillis() + 86400000L - 1L);
			interval.setEndInString(
					DateUtils.formatDate(calendar.getTimeInMillis() + 86400000L - 1L, "yyyy-MM-dd HH:mm:ss.SSS"));
		}
		return interval;
	}

}

  4,java調用app

public static void main(String[] args)
	{
		//當前時間
		Long currentTimeMillis = System.currentTimeMillis();
		// 30天前時間
		TimeIntervalVO beginTime = null;
		beginTime = TimeUtils.getMillisInterval(currentTimeMillis, -30);
		// 昨天時間
		TimeIntervalVO endTime = null;
		endTime = TimeUtils.getMillisInterval(currentTimeMillis, -1);
		System.out.println(beginTime.toString());
		System.out.println(endTime.toString());
		
	}

顯示結果 eclipse

 

相關文章
相關標籤/搜索