ps:代码有参考别人的,非全部原创。已在正式项目上使用

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/**

* 获取  当前年、半年、季度、月、日、小时 开始结束时间

*/

private final SimpleDateFormat shortSdf;

private final SimpleDateFormat longHourSdf;

private final SimpleDateFormat longSdf;

public RemindDateUtils(){

this.shortSdf = new SimpleDateFormat("yyyy-MM-dd");

this.longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");

this.longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

}

/**

* 获得本周的第一天,周一

*

* @return

*/

public   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   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();

}

/**

* 获得本天的开始时间,即2012-01-01 00:00:00

*

* @return

*/

public   Date getCurrentDayStartTime() {

Date now = new Date();

try {

now = shortSdf.parse(shortSdf.format(now));

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

/**

* 获得本天的结束时间,即2012-01-01 23:59:59

*

* @return

*/

public   Date getCurrentDayEndTime() {

Date now = new Date();

try {

now = longSdf.parse(shortSdf.format(now) + " 23:59:59");

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

/**

* 获得本小时的开始时间,即2012-01-01 23:59:59

*

* @return

*/

public   Date getCurrentHourStartTime() {

Date now = new Date();

try {

now = longHourSdf.parse(longHourSdf.format(now));

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

/**

* 获得本小时的结束时间,即2012-01-01 23:59:59

*

* @return

*/

public   Date getCurrentHourEndTime() {

Date now = new Date();

try {

now = longSdf.parse(longHourSdf.format(now) + ":59:59");

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

/**

* 获得本月的开始时间,即2012-01-01 00:00:00

*

* @return

*/

public   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;

}

/**

* 当前月的结束时间,即2012-01-31 23:59:59

*

* @return

*/

public   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;

}

/**

* 当前年的开始时间,即2012-01-01 00:00:00

*

* @return

*/

public   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;

}

/**

* 当前年的结束时间,即2012-12-31 23:59:59

*

* @return

*/

public   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;

}

/**

* 当前季度的开始时间,即2012-01-1 00:00:00

*

* @return

*/

public   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;

}

/**

* 当前季度的结束时间,即2012-03-31 23:59:59

*

* @return

*/

public   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,

;

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   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   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;

}

}

/**

* 获取前一天日期 及星期

*/

public class RemindDateUtils {

private void initDate(){

String[] weekDays = {"周日","周一","周二","周三","周四","周五","周六"};

Calendar cal = Calendar.getInstance();

cal.add(Calendar.Date,-1);

int i = cal.get(Calendar.DAY_OF_WEEK)-1;

if(i<0){

i=0;

}

String yesterday = new SimpleDateFormate("yyyy年MM月dd日").format(cal.getTime());

String w = weekDays[i];

}

}

PS:SimpleDateFormat是线程不安全的

java 获取年和季度_java获取当前年、半年、季度、月、日、小时 开始结束时间等...相关推荐

  1. JAVA获取一个月的开始与结束时间以及两个日期相差几个月

    一.一个月的开始与结束时间代码如下 /*** 获取指定月的开始日期* @param currentDate* @return*/public static Date getStartDate(Stri ...

  2. php 上一个月的开始和结束,php获取上一个月的开始与结束时间遇到的问题

    改正之前: $_lastMonthStart = date('Y-m-1 00:00:00', strtotime("-1 month")); $_lastMonthEnd = d ...

  3. java 本季度_Java获取当天、本周、本月、本季度、本年等 开始及结束时间

    package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import jav ...

  4. java获取文件地址吗_java获取文件所在服务器位置路径

    1.通过 import javax.servlet.ServletContext 类获取web 服务器所在真实路径 public ActionForward execute( ActionMappin ...

  5. java ip地址接口_java获取ip地址与网络接口的方法示例

    java.net包 大家应该都知道,网络相关对象在java.net包中,Java net包下的类如下: 1.获取主机对象InetAddress //获取本地主机对象 InetAddress host ...

  6. java获取扩展名_Java获取文件扩展名称

    有时在处理文件时,需要根据文件类型对它们进行不同的处理. java.io.File没有任何获取文件扩展名的方法,这里提供了一个实用工具方法来获取文件扩展名. Java获取文件扩展名 在句点(.)之后, ...

  7. java 获取秒数_Java获取精确到秒的时间戳(转)

    1.时间戳简介: 时间戳的定义:通常是一个字符序列,唯一地标识某一刻的时间.数字时间戳技术是数字签名技术一种变种的应用.是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01 ...

  8. 轻松搞定-根据当前日期获取今日、昨日、本周、上周、本月、上月开始结束时间

    1.只需要传入type参数,即可自动判断得出开始结束时间: type:今日:today.昨日:yesterday.本周:thisweek.上周:lastweek.本月:thismonth. 上月:la ...

  9. 计算当前周、月、季、半年、一年和上周、上月、上季、当前半年的上一个半年、上年的开始和结束时间

    下面的例子是一个完整的工具类,记录了各种获取开始时间和结束时间的方法,其中本人用过的个别方法为开始时间和结束时间格式为 2019-10-10 00:00:00 ,2019-10-10 23:59:59 ...

最新文章

  1. sql移动加权计算利润_一文搞懂股票指数的4种加权方式
  2. 1.6 三维卷积-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  3. 中科大计算机学院的保研率,中科大2021届保研率44.7%,少年班83.4%,物理计算机人数最多...
  4. Mysql常用命令详解
  5. 2015年10月15日学习html基础笔记
  6. 2020牛客多校2 - Exclusive OR(FWT)
  7. 感知算法论文(九):Towards Accurate One-Stage Object Detection with AP-Loss
  8. IntelliJ IDEA内部设计
  9. 吴恩达,确诊新冠阳性!
  10. 2005年博客发展十大悬疑
  11. 算法(三):图解广度优先搜索算法
  12. [Z]四种浏览器对 clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth 和 scrollWidth 的解释...
  13. 物联网核心安全系列——智能家居与数据安全问题
  14. 爬虫项目——xpath练手(1)
  15. 移植 Qt4.8.5到Tiny210
  16. php 文件预览 水印,php实现在线预览word等office文件,同时添加水印
  17. Java 私塾面试系列
  18. windows安装hbase1.4.9
  19. LoadRunner教程(12)-LoadRunner IP欺骗
  20. openh264限制slice/nal分片大小导致的编码数据错误

热门文章

  1. DataAnalysis:数据分析、数据清理、数据合并
  2. HTML5-画一个简单五角星
  3. Python绘制图像的灰度直方图、累计直方图
  4. trello 开源_Trello的5种开源替代品
  5. CSS行高(line-height)使文本垂直居中详解
  6. 安装ubantu18.04到移动硬盘
  7. html中canvas设置透明度,HTML5 Canvas绘制时指定颜色与透明度的方法
  8. 【C++】RC4加密算法
  9. 当前开发者未绑定此appid,请到小程序管理
  10. G1垃圾收集器原理剖析【官方解读】