TmsTimeUtils 时间戳
package com.sprucetec.tms.utils; import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* 时间工具类
*
* Title: TmsTimeUtils.java<br>
* Description: <br
*/
public class TmsTimeUtils { /**
* 根据传入的时间字符串,获得unix对应的时间戳格式
*
* @author liuqiang(liuqang@meicai.cn)
* 2016年3月12日
* @param day
* @return
*/
public static Integer getDayUnixTimeStamp(String day) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = df.parse(day);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
long timestamp = cal.getTimeInMillis();
return Integer.valueOf((int) (timestamp / 1000));
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
} /**
* 将日期转换为 时间戳
* @author yangweiqiang 2016.12.01
* @param date
* @return
*/
public static Integer getDayUnixTimeStamp(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String day = df.format(date);
return getDayUnixTimeStamp(day);
} /**
* 获取当天日期的unix时间戳
*
* @author liuqiang(liuqiang@meicai.cn)
* 2016年3月12日
* @return
*/
public static Integer getTodayUnixTimeStamp() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String day = df.format(date);
return getDayUnixTimeStamp(day);
} /**
* 获得当前系统时间戳
* @author liuqiang(liuqiang@meicai.cn)
* 2016年3月20日
* @return
*/
public static Integer getNowTimeStamp() {
Integer now = 0;
Long time = System.currentTimeMillis() / 1000;
now = time.intValue();
return now;
} /**
* 描述: 将秒转换为指定格式化的日期
* @author yangweiqiang
* @param timeStamp 秒数
* @param format 格式化 yyyy-MM-dd等
* @date 2016/8/15
*/
public static String getFormatDate(Integer timeStamp,SimpleDateFormat format){
return format.format(new Date(timeStamp * 1000L));
} /**
* 获取某月最大的天数
* @author yangweiqiang
* @param time 日期
* @param format 日期格式化类型
* @date 2016/8/3
* @return 实际最大天数
*/
public static int getMaxDayOfMonth(String time,SimpleDateFormat format){
try {
Date date = format.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
} catch (ParseException e) {
e.printStackTrace();
} return 0;
} /**
* 获取昨天的时间戳
* @return
*/
public static int getPreDayByToday(Integer days){
return getTodayUnixTimeStamp() + 86400 * days;
} /**
* 获取指定天的推迟天时间戳
* @param time
* @param days
* @return
*/
public static int getPreDayBy(Integer time,Integer days){
return time + 86400 * days;
} /**
* 获取指定天的推迟天时间戳
* @param time
* @param days 增加的天数
* @return
*/
public static int getPreDayBy(String time,Integer days){
return getDayUnixTimeStamp(time) + 86400 * days;
} /**
* 根据时间戳获取日期(此日期为几号)
* @param date
* @return
*/
public static int getDayBy(Integer date){
Calendar cal = Calendar.getInstance();
cal.setTime(new Date((long)date * 1000));
return cal.get(Calendar.DAY_OF_MONTH);
} /**
* 获取上个月第一天的Unix时间戳
*/
public static Integer getLastMonthFirstDayUnixTimeStamp() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
Date lastMonthFirstDay = cal.getTime();
return TmsTimeUtils.getDayUnixTimeStamp(lastMonthFirstDay);
} /**
* 获取上个月最后一天的Unix时间戳
*/
public static Integer getLastMonthLastDayUnixTimeStamp() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
Date lastMonthLastDay = cal.getTime();
return TmsTimeUtils.getDayUnixTimeStamp(lastMonthLastDay);
} /**
* 获取本月第一天的Unix时间戳
*/
public static Integer getThisMonthFirstDayUnixTimeStamp() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
Date thisMonthFirstDay = cal.getTime();
return TmsTimeUtils.getDayUnixTimeStamp(thisMonthFirstDay);
} public static void main(String[] args) {
// System.out.println(TmsTimeUtils.getDayUnixTimeStamp("2016-03-12"));
// System.out.println(TmsTimeUtils.getTodayUnixTimeStamp());
System.out.println(getLastMonthFirstDayUnixTimeStamp());
System.out.println(getLastMonthLastDayUnixTimeStamp());
System.out.println(getThisMonthFirstDayUnixTimeStamp());
}
}
TmsTimeUtils 时间戳的更多相关文章
- C# DateTime与时间戳转换
C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳: ...
- nodejs中获取时间戳、时间差
Nodejs中获取时间戳的方法有很多种,例如: new Date().getTime() Date.now() process.uptime() process.hrtime() 平时想获取一个时间戳 ...
- EF里Guid类型数据的自增长、时间戳和复杂类型的用法
通过前两章Lodging和Destination类的演示,大家肯定基本了解Code First是怎么玩的了,本章继续演示一些很实用的东西.文章的开头提示下:提供的demo为了后面演示效果,前面代码有些 ...
- fmt标签把时间戳格式化日期
jsp页面标签格式化日期 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %> ...
- MySQL对时间戳的转换处理
开发中很多时候在数据库里都会存储Long类型的时间戳,而时间戳做比对会相对麻烦 我的绝决方案: SELECT FROM_UNIXTIME(LEFT(create_time,10), '%Y-%m-%d ...
- Kafka消息时间戳(kafka message timestamp)
最近碰到了消息时间戳的问题,于是花了一些功夫研究了一下,特此记录一下. Kafka消息的时间戳 在消息中增加了一个时间戳字段和时间戳类型.目前支持的时间戳类型有两种: CreateTime 和 L ...
- Python时间戳和日期的相互转换
Python时间戳和日期的相互转换 (2014-03-17 11:24:35) 转载▼ 分类: Python 当前时间戳:time.time() 当前日期:time.ctime() 1.Pytho ...
- 时间戳TimeStamp处理
我获得这个时间戳是得想除以1000再处理的,看看你们的需要先除多少再处理 //时间戳处理 NSInteger time = timeStamp / 1000; NSNumber *timer = [ ...
- C#中DateTime.Ticks属性及Unix时间戳转换
1.相关概念 DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). ...
随机推荐
- 使用SpringMVC的@CrossOrigin注解解决跨域请求问题
跨域问题,通俗说就是用ajax请求其他站点的接口,浏览器默认是不允许的.同源策略(Same-orgin policy)限制了一个源(orgin)中加载脚本或脚本与来自其他源(orgin)中资源的交互方 ...
- 2019.01.02 bzoj2467: [中山市选2010]生成树(矩阵树定理)
传送门 矩阵树定理模板题. 题意简述:自己看题面吧太简单懒得写了 直接构建出这4n4n4n个点然后按照题面连边之后跑矩阵树即可. 代码: #include<bits/stdc++.h> # ...
- 2018.11.24 spoj New Distinct Substrings(后缀数组)
传送门 双倍经验(弱化版本) 考虑求出来heightheightheight数组之后用增量法. 也就是考虑每增加一个heightheightheight对答案产生的贡献. 算出来是∑∣S∣−heigh ...
- 2018.10.26 NOIP训练 数数树(换根dp)
传送门 换根dpdpdp傻逼题好像不好码啊. 考虑直接把每一个二进制位拆开处理. 先dfsdfsdfs出每个点到1的异或距离. 然后分类讨论一波: 如果一个点如果当前二进制位到根节点异或距离为1,那么 ...
- maven 中央仓库地址 随笔记下了
Maven 中央仓库地址: 1. http://www.sonatype.org/nexus/ 2. http://mvnrepository.com/ 3. http://repo1.maven.o ...
- JPA错误
2016-11-141.2016-10-31: hibernate用注解 一对多 报Could not determine type for错误 原因: 接下来继续解决第二个问题:怎么又与集合打交道 ...
- C++STL 容器比较
Vector的使用场景:比如软件历史操作记录的存储,我们经常要查看历史记录,比如上一次的记录,上上次的记录,但却不会去删除记录,因为记录是事实的描述. deque的使用场景:比如排队购票系统,对排队者 ...
- myeclipse cannot connect to vm
启动tomcat时,tomcat可以直接运行,而debug时弹出 解决方法:打开360安全卫士的功能大全找到修复网络(LSP)点击立即修复就可以使用debug
- application.properties /application.yml官网查看配置;springboot application.properties 官网查看,info yml 查看;springboot.yml查看info;springboot.yml查看Actuator监控中心info
官网查看: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#appendix 查看info ...
- POJ3258--River Hopscotch(Binary Search similar to POJ2456)
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully ...