java设置时间偏移天数,

1,

1package com.banksteel.openerp.common.vo; 2 3import java.io.Serializable; 4 5public class TimeIntervalVO implements Serializable { 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = -1568237892596588268L; 11 private Long beginInMillis; // 设置时间当天的零点 12 private Long endInMillis; // 设置时间当天最后一秒 13 private String beginInString; // 显示时间 14 private String endInString; // 显示时间 15 16 public Long getBeginInMillis() { 17 return beginInMillis; 18 } 19 20 public void setBeginInMillis(Long beginInMillis) { 21 this.beginInMillis = beginInMillis; 22 } 23 24 public Long getEndInMillis() { 25 return endInMillis; 26 } 27 28 public void setEndInMillis(Long endInMillis) { 29 this.endInMillis = endInMillis; 30 } 31 32 public String getBeginInString() { 33 return beginInString; 34 } 35 36 public void setBeginInString(String beginInString) { 37 this.beginInString = beginInString; 38 } 39 40 public String getEndInString() { 41 return endInString; 42 } 43 44 public void setEndInString(String endInString) { 45 this.endInString = endInString; 46 } 47 48 @Override 49 public String toString() { 50 return "TimeIntervalVO [beginInMillis=" + beginInMillis + ", endInMillis=" + endInMillis + ", beginInString=" 51 + beginInString + ", endInString=" + endInString + "]"; 52 } 53 54}

  2.

1/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. 2 3package cn.mysteel.util; 4 5import java.text.SimpleDateFormat; 6import java.util.Date; 7import org.apache.commons.lang.StringUtils; 8 9public class DateUtils 10{ 11 12 public DateUtils() 13 { 14 } 15 16 public static Date getDate(String s) 17 { 18 return getDate(s, null); 19 } 20 21 public static Date getJustDate(String s) 22 { 23 return getDate(s, "yyyy-MM-dd"); 24 } 25 26 public static Date getDate(long date) 27 { 28 return getDate(date, null); 29 } 30 31 public static Date getJustDate(long date) 32 { 33 return getDate(date, "yyyy-MM-dd"); 34 } 35 36 public static Date getDate(long date, String format) 37 { 38 if(StringUtils.isEmpty(format)) 39 format = "yyyy-MM-dd HH:mm:ss"; 40 return getDate(formatDate(new Date(date), format), format); 41 } 42 43 public static Date getDate(String s, String format) 44 { 45 Date date; 46 try 47 { 48 if(StringUtils.isEmpty(format)) 49 format = "yyyy-MM-dd HH:mm:ss"; 50 date = (new SimpleDateFormat(format)).parse(s); 51 } 52 catch(Exception e) 53 { 54 date = new Date(0L); 55 } 56 return date; 57 } 58 59 public static String formatDate(long date, String format) 60 { 61 return formatDate(new Date(date), format); 62 } 63 64 public static String formatDate(long date) 65 { 66 return formatDate(new Date(date), null); 67 } 68 69 public static String formatJustDate(long date) 70 { 71 return formatDate(new Date(date), "yyyy-MM-dd"); 72 } 73 74 public static String formatDate(Date date, String format) 75 { 76 if(StringUtils.isEmpty(format)) 77 format = "yyyy-MM-dd HH:mm:ss"; 78 return (new SimpleDateFormat(format)).format(date); 79 } 80 81 public static final String DATE_FROMAT1 = "yyyy-MM-dd"; 82 public static final String DATE_FROMAT2 = "yyyy-MM-dd HH:mm:ss"; 83} 84 85 86/* 87 DECOMPILATION REPORT 88 89 Decompiled from: D:\eclipse\demo-maven\repository\cn\mysteel\shgl-core\1.0.0-SNAPSHOT\shgl-core-1.0.0-SNAPSHOT.jar 90 Total time: 321 ms 91 Jad reported messages/errors: 92 Exit status: 0 93 Caught exceptions: 94*/

  3

1package com.banksteel.openerp.utils; 2 3import java.util.Calendar; 4 5import com.banksteel.openerp.common.vo.TimeIntervalVO; 6 7import cn.mysteel.util.DateUtils; 8 9/** 10 * 时间工具类 11 * 12 * @description: 13 * @projectName:openerp-webapp 14 * @className:TimeUtils.java 15 * @createTime:2016年9月27日 下午4:02:40 16 * @version 1.0 17 */ 18public class TimeUtils { 19 20 private TimeUtils() { 21 }; 22 23 /** 24 * 获取基准时间戳计算指定天数后的当天00:00:00.000至23:59:59.999时间戳区间 25 * 26 * @param millis 27 * 基准时间戳 28 * @param dayOffset 29 * 偏移天数,0表示当天,正数表示基准时间戳之后N天,负数反之 30 * @createTime:2016年9月29日 下午5:19:40 31 */ 32 public static TimeIntervalVO getMillisInterval(Long millis, int dayOffset) { 33 TimeIntervalVO interval = new TimeIntervalVO(); 34 Calendar calendar = Calendar.getInstance(); 35 if (millis > 0) { 36 calendar.setTime(DateUtils.getDate(millis, "yyyy-MM-dd")); 37 calendar.add(Calendar.DATE, dayOffset); 38 interval.setBeginInMillis(calendar.getTimeInMillis()); 39 interval.setBeginInString(DateUtils.formatDate(calendar.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss.SSS")); 40 interval.setEndInMillis(calendar.getTimeInMillis() + 86400000L - 1L); 41 interval.setEndInString( 42 DateUtils.formatDate(calendar.getTimeInMillis() + 86400000L - 1L, "yyyy-MM-dd HH:mm:ss.SSS")); 43 } 44 return interval; 45 } 46 47}

  4,java调用

1public static void main(String[] args) 2 { 3 //当前时间 4 Long currentTimeMillis = System.currentTimeMillis(); 5 // 30天前时间 6 TimeIntervalVO beginTime = null; 7 beginTime = TimeUtils.getMillisInterval(currentTimeMillis, -30); 8 // 昨天时间 9 TimeIntervalVO endTime = null; 10 endTime = TimeUtils.getMillisInterval(currentTimeMillis, -1); 11 System.out.println(beginTime.toString()); 12 System.out.println(endTime.toString()); 13 14 }

显示结果 

 

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

Python之time模块的时间戳、时间字符串格式化与转换

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y

mysql设置时区

mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0