Java获取当天、当前月、当前年(今年)的开始和结束时间戳
最近在做统计相关的功能的时候涉及到了获取当天的开始和结束的时间戳、当月和当年的开始结束时间戳,特此记录,以作备忘。
相关代码
package com.lingyejun.authenticator; import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.TimeZone; public class CalendarAdjust { /**
* 获取指定某一天的开始时间戳
*
* @param timeStamp 毫秒级时间戳
* @param timeZone 如 GMT+8:00
* @return
*/
public static Long getDailyStartTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
} /**
* 获取指定某一天的结束时间戳
*
* @param timeStamp 毫秒级时间戳
* @param timeZone 如 GMT+8:00
* @return
*/
public static Long getDailyEndTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
} /**
* 获取当月开始时间戳
*
* @param timeStamp 毫秒级时间戳
* @param timeZone 如 GMT+8:00
* @return
*/
public static Long getMonthStartTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
} /**
* 获取当月的结束时间戳
*
* @param timeStamp 毫秒级时间戳
* @param timeZone 如 GMT+8:00
* @return
*/
public static Long getMonthEndTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
} /**
* 获取当年的开始时间戳
*
* @param timeStamp 毫秒级时间戳
* @param timeZone 如 GMT+8:00
* @return
*/
public static Long getYearStartTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.DATE, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
} /**
* 获取当年的最后时间戳
*
* @param timeStamp 毫秒级时间戳
* @param timeZone 如 GMT+8:00
* @return
*/
public static Long getYearEndTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
int year = calendar.get(Calendar.YEAR);
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
return calendar.getTimeInMillis();
} /**
* 时间戳转字符串
*
* @param timestamp 毫秒级时间戳
* @param zoneId 如 GMT+8或UTC+08:00
* @return
*/
public static String timestampToStr(long timestamp, String zoneId) {
ZoneId timezone = ZoneId.of(zoneId);
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), timezone);
return localDateTime.toString();
} public static void main(String[] args) {
Long currentTime = System.currentTimeMillis();
System.out.println("Current Time : " + currentTime + " = " + timestampToStr(currentTime, "GMT+8"));
Long dailyStart = getDailyStartTime(currentTime, "GMT+8:00");
Long dailyEnd = getDailyEndTime(currentTime, "GMT+8:00");
Long monthStart = getMonthStartTime(currentTime, "GMT+8:00");
Long monthEnd = getMonthEndTime(currentTime, "GMT+8:00");
Long yearStart = getYearStartTime(currentTime, "GMT+8:00");
Long yearEnd = getYearEndTime(currentTime, "GMT+8:00"); System.out.println("Daily Start : " + dailyStart + " = " + timestampToStr(dailyStart, "GMT+8") + " Daily End : " + dailyEnd + " = " + timestampToStr(dailyEnd, "GMT+8"));
System.out.println("Month Start : " + monthStart + " = " + timestampToStr(monthStart, "GMT+8") + " Month End : " + monthEnd + " = " + timestampToStr(monthEnd, "GMT+8"));
System.out.println("Year Start : " + yearStart + " = " + timestampToStr(yearStart, "GMT+8") + " Year End : " + yearEnd + " = " + timestampToStr(yearEnd, "GMT+8"));
}
}
效果
Java获取当天、当前月、当前年(今年)的开始和结束时间戳的更多相关文章
- java获取当天,前天,明天,本周,本月,本年的开始日期时间和结束日期时间
package demoone; import java.sql.Timestamp; import java.text.ParseException; import java.text.Simple ...
- Java获取上周,本周,本月,本年,开始结束时间 。日期工具类
由于获取日期经常会使用到,所有我自己写了一个工具类 1.基本上能用上的都写出来了,包括:1)获取当天的开始时间 2)获取当天的结束时间 3)获取昨天的开始时间 4)获取昨天的结束时间 5)获取明天的开 ...
- Java获取当天、本周、本月、本季度、本年等 开始及结束时间
package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import jav ...
- Java获取当天或者明天等零点时间(00:00:00)0时0分0秒的方法
SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = ...
- java 获取当天(今日)零点零分零秒
两种方法 一种得到的是时间戳,一种得到是日期格式: 1.日期格式的 Calendar calendar = Calendar.getInstance(); calendar.setTime(new D ...
- tp5.1中返回当天、昨天、当月等的开始和结束时间戳
/** * 返回今日开始和结束的时间戳 * * @return array */function today(){ list($y, $m, $d) = explode('-', date('Y-m- ...
- 【原创】java 获取十个工作日之前或之后的日期(算当天)-完美解决-费元星
[原创]java 获取十个工作日之后的日期(算当天)-完美解决-费元星(仅考虑星期六星期天) /** * * 根据开始日期 ,需要的工作日天数 ,计算工作截止日期,并返回截止日期 * @param s ...
- Java中常用的获取从当前月开始的前第i个月、取结束时间与开始时间相差多少个月份等的方法
@RunWith(SpringRunner.class) @SpringBootTest public class DateTest { @Test public void test(){ DateF ...
- Java获取各种常用时间方法大全
Java获取各种常用时间方法大全 package cc.javaweb.test; Java中文网,Java获取各种时间大全 import java.text.DateFormat; import j ...
随机推荐
- 图片Image转换为base64编码的方法
1.FileReader 通过XMLHttpRequest请求图片Blob数据格式,然后利用FileReader转换为dataURL function toDataURL(url, callback) ...
- centos7 install mysql5.7.27
1.yum 安装 wget yum install wget 2.下载MySQL 的yum repo wget https://repo.mysql.com//mysql57-community-re ...
- nginx配置访问xx.com跳转www.xx.com
二.在nginx里面配置 rewrite 规则.打开 Nginx.conf 文件找到server配置段:[以下是我的server配置段] 禁止IP地址访问 server{ listen 80 defa ...
- Hybris订单价格的折扣维护
backoffice里创建一个新订单,维护一个行项目,添加一个产品: 在行项目的SubTotal界面,维护Base Price,在Discount values字段里,输入折扣信息:discount: ...
- 大数据量高并发的数据库优化,sql查询优化
一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. ...
- Python爬虫系列:五、正则表达式
1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 正则表达式 ...
- 浅谈HDFS(三)之DataNote
DataNode工作机制 一个数据块在DataNode上以文件形式存储在磁盘上,包括两个文件,一个是数据本身,一个是元数据包括数据块的长度,块数据的校验和,以及时间戳. DataNode启动后向Nam ...
- C语言基础知识-程序流程结构
C语言基础知识-程序流程结构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.概述 C语言支持最基本的三种程序运行结构:顺序结构,选择结构,循环结构. 顺序结构:程序按顺序执行, ...
- Kubernetes网络之Flannel工作原理
目录 1.Docker网络模式 1.1 bridge网络的构建过程 1.2 外部访问 2.Kubernetes网络模式 2.1 同一个Pod中容器之间的通信 2.2 不同Pod中容器之间的通信 2.3 ...
- jetbreains的crack方法
https://zhile.io/2018/08/20/jetbrains-license-server-crack.html