java获取当前时间的年周月季度等的开始结束时间
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* Created by xiaochun on 2016/3/24.
*/
public class TimeUtil {
public static void main(String[] args) {
System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
} /**
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
*/ private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");;
private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");; /**
* 获得本周的第一天,周一
*
* @return
*/
public static Date getCurrentWeekDayStartTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
c.add(Calendar.DATE, -weekday);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
} /**
* 获得本周的最后一天,周日
*
* @return
*/
public static Date getCurrentWeekDayEndTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
} /**
* 获得本天的开始时间
*
* @return
*/
public static Date getCurrentDayStartTime() {
Date now = new Date();
try {
now = shortSdf.parse(shortSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 获得本天的结束时间
*
* @return
*/
public static Date getCurrentDayEndTime() {
Date now = new Date();
try {
now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 获得本小时的开始时间
*
* @return
*/
public static Date getCurrentHourStartTime() {
Date now = new Date();
try {
now = longHourSdf.parse(longHourSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 获得本小时的结束时间
*
* @return
*/
public static Date getCurrentHourEndTime() {
Date now = new Date();
try {
now = longSdf.parse(longHourSdf.format(now) + ":59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 获得本月的开始时间
*
* @return
*/
public static Date getCurrentMonthStartTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.DATE, 1);
now = shortSdf.parse(shortSdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 本月的结束时间
*
* @return
*/
public static Date getCurrentMonthEndTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.DATE, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 当前年的开始时间
*
* @return
*/
public static Date getCurrentYearStartTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.MONTH, 0);
c.set(Calendar.DATE, 1);
now = shortSdf.parse(shortSdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 当前年的结束时间
*
* @return
*/
public static Date getCurrentYearEndTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 当前季度的开始时间
*
* @return
*/
public static Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 4);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 当前季度的结束时间
*
* @return
*/
public static Date getCurrentQuarterEndTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
} /**
* 获取前/后半年的开始时间
*
* @return
*/
public static Date getHalfYearStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 6) {
c.set(Calendar.MONTH, 0);
} else if (currentMonth >= 7 && currentMonth <= 12) {
c.set(Calendar.MONTH, 6);
}
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now; } /**
* 获取前/后半年的结束时间
*
* @return
*/
public static Date getHalfYearEndTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
}
参考:https://www.cnblogs.com/duguxiaobiao/p/9128835.html?tdsourcetag=s_pcqq_aiomsg
java获取当前时间的年周月季度等的开始结束时间的更多相关文章
- PHP获取一年有几周以及每周开始日期和结束日期
最近接了一个项目,其中有一需求是用php获取一年有几周以及每周开始日期和接触日期.在网上找些资料没有合适的,于是自己做了一份,下面通过两种方式实现PHP获取一年有几周以及每周开始日期和结束日期 代码一 ...
- (干货)java中如何根据一个时间获取属于本年那一周,本周的开始时间以及最后一天时间。并且设置起始时间为周6.结束时间为周5
本人亲测,有用,适用性比较强,直接上代码说话. package com.helloBike.data; import java.text.ParseException; import java.tex ...
- SQL获取本周,上周,本月,上月的开始时间和结束时间
),),--本周 ),),--上周 ),),--本月 ),),--上月 ),),--近半年 ),)--近一年 ), ,, ),)--本周开始时间 ), ,, ),)--本周结束时间 ),,, ),)- ...
- java获取当前时间前一周、前一月、前一年的时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calend ...
- java获取指定日期的年、月、日的值
参数:String dateStr = '2016-05-18'; 1.获取string对应date日期: Date date = new SimpleDateFormat("yyyy-MM ...
- Java-小技巧-004-jdk时间,jdk8时间,joda,calendar,获取当前时间前一周、前一月、前一年的时间
1.推荐使用java8 localdate等 线程安全 支持较好 地址 2.joda 一.简述 查看SampleDateFormat源码,叙述有: * Date formats are not syn ...
- DB2获取有效工作时长函数(排除节假日、排除午休时间)
CREATE OR REPLACE FUNCTION DIFFHOURTIME_WITHOUTHOLIDAY_FUN ( STARTTIME ), ENDTIME ) ) RETURNS DOUBLE ...
- C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
一:C# 获取两个时间段之间的所有时间 public List<string> GetTimeList(string rq1, string rq2) { List<string&g ...
- js 获取开始时间和结束时间相隔小时及分钟(时间戳操作)
js 获取开始时间和结束时间相隔小时及分钟(时间戳操作) 场景描述:获取开始时间和结束时间相隔小时及分钟 实例: TimeOnConfirm(curDate) { if(this.pickernum ...
随机推荐
- python2和python3中split的坑
执行同样的split,python2和python3截取的行数内容却不一样 我想要截取Dalvik Heap行,使用split('\n')的方法 import os cpu='adb shell du ...
- 【Linux开发】V4L2驱动框架分析学习
Author:CJOK Contact:cjok.liao#gmail.com SinaWeibo:@廖野cjok 1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上 ...
- 深入理解Linux-hostname
当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...
- java中重写
1.重写[针对父类与子类而言]---------即java的多态性[子类与父类间有相同的名称和参数,此方法就被重写Overriding:又称:方法覆盖] 子类对父类的允许访问的方法的实现过程进行重新编 ...
- P2010回文日期
这道题是2016年普及组的题,难度等级为普及-. 这道题仍然是个模拟题.有两种策略:1.枚举回文,看日期是否存在2.枚举日期,看是否是回文.显然,前者要快很多,并且准确.本蒟蒻第一次便使用了后者,bu ...
- 多條件查詢SQL語句
表结构如下: –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_i ...
- vmware下使用nat方法联网
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Li_Zefeng/article/det ...
- 阿里云服务器重启出现An error occurred 如何处理
最近网站重启阿里云服务后,出现 An error occurred, An error occurred. Sorry, the page you are looking for is current ...
- python数据结构:pandas(2)数据操作
一.Pandas的数据操作 0.DataFrame的数据结构 1.Series索引操作 (0)Series class Series(base.IndexOpsMixin, generic.NDFra ...
- octave - 用于数值计算的高级交互式语言
SYNOPSIS 总览 octave [options] OPTIONS 选项 octave 全部命令行选项可以通过运行命令 octave --help 来查看. DESCRIPTION 描述 Oct ...