package demoone;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List; public class DateUtils {
//获取当天的开始时间
public static java.util.Date getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
//获取当天的结束时间
public static java.util.Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
//获取昨天的开始时间
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取昨天的结束时间
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取明天的开始时间
public static Date getBeginDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime();
}
//获取明天的结束时间
public static Date getEndDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
//获取本周的开始时间
public static Date getBeginDayOfWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
//获取本周的结束时间
public static Date getEndDayOfWeek(){
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
//获取本月的开始时间
public static Date getBeginDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
//获取本月的结束时间
public static Date getEndDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
//获取本年的开始时间
public static java.util.Date getBeginDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
// cal.set
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1); return getDayStartTime(cal.getTime());
}
//获取本年的结束时间
public static java.util.Date getEndDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
}
//获取某个日期的开始时间
public static Timestamp getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
//获取某个日期的结束时间
public static Timestamp getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
//获取今年是哪一年
public static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
//获取本月是哪一月
public static int getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
//两个日期相减得到的天数
public static int getDiffDays(Date beginDate, Date endDate) { if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
} long diff = (endDate.getTime() - beginDate.getTime())
/ (1000 * 60 * 60 * 24); int days = new Long(diff).intValue(); return days;
}
//两个日期相减得到的毫秒数
public static long dateDiff(Date beginDate, Date endDate) {
long date1ms = beginDate.getTime();
long date2ms = endDate.getTime();
return date2ms - date1ms;
}
//获取两个日期中的最大日期
public static Date max(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return beginDate;
}
return endDate;
}
//获取两个日期中的最小日期
public static Date min(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return endDate;
}
return beginDate;
}
//返回某月该季度的第一个月
public static Date getFirstSeasonDate(Date date) {
final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int sean = SEASON[cal.get(Calendar.MONTH)];
cal.set(Calendar.MONTH, sean * 3 - 3);
return cal.getTime();
}
//返回某个日期下几天的日期
public static Date getNextDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
return cal.getTime();
}
//返回某个日期前几天的日期
public static Date getFrontDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
return cal.getTime();
}
//获取某年某月到某年某月按天的切片日期集合(间隔天数的日期集合)
public static List getTimeList(int beginYear, int beginMonth, int endYear,
int endMonth, int k) {
List list = new ArrayList();
if (beginYear == endYear) {
for (int j = beginMonth; j <= endMonth; j++) {
list.add(getTimeList(beginYear, j, k)); }
} else {
{
for (int j = beginMonth; j < 12; j++) {
list.add(getTimeList(beginYear, j, k));
} for (int i = beginYear + 1; i < endYear; i++) {
for (int j = 0; j < 12; j++) {
list.add(getTimeList(i, j, k));
}
}
for (int j = 0; j <= endMonth; j++) {
list.add(getTimeList(endYear, j, k));
}
}
}
return list;
}
//获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
public static List getTimeList(int beginYear, int beginMonth, int k) {
List list = new ArrayList();
Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
int max = begincal.getActualMaximum(Calendar.DATE);
for (int i = 1; i < max; i = i + k) {
list.add(begincal.getTime());
begincal.add(Calendar.DATE, k);
}
begincal = new GregorianCalendar(beginYear, beginMonth, max);
list.add(begincal.getTime());
return list;
}
}

java DateUtils的更多相关文章

  1. Java DateUtils 的实用

    Java DateUtils的实用可以很方便的对日期进行对年,月,日,时,分,秒的相加和相减,能很好的解决日期的运算 可以不用必须调用Oracle等数据库本地函数进行运算,相比之下更为简洁方便. pa ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. java使用DateUtils对日期进行运算

    最近在写数据上传的程序,需要对Date进行一些数学运算,个人感觉在java中,日期的数学运算还是比较常用的,所以把Date的数学运算都玩了一下.试了一下,发现DateUtils这个工具类对于Date的 ...

  4. java 日期工具类DateUtils

      日期工具类DateUtils CreateTime--2017年5月27日08:48:00Author:Marydon DateUtils.java-对日期类的进一步封装 import java. ...

  5. JAVA 日期处理工具类 DateUtils

    package com.genlot.common.utils; import java.sql.Timestamp;import java.text.ParseException;import ja ...

  6. java的TimeUtils或者DateUtils的编写心得

    一.几种常见的日期和时间类介绍 介绍时间工具类不可避免必须要去触碰几个常见的日期和时间类,所以就简单介绍一下. 1.jdk1.8之前的日期时间类 a.Date类 我们可以通过new的方式生成一个Dat ...

  7. java时间处理工具类--DateUtils

    package com.hexiang.utils; /** * @(#)DateUtil.java * * * @author kidd * @version 1.00 2007/8/8 */ im ...

  8. java日期的运用(DateUtils工具类)

    public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...

  9. jdk8 之 java.time包AND DateUtils

    package com.jansh.comm.util; import java.time.Clock; import java.time.LocalDate; import java.time.Lo ...

随机推荐

  1. 第n+1次考试

    题目: 1. 中位数 [问题描述] 给定C个不同物品,每个物品有一重量和体积,保证每个物品的重量不一样.从中选出N个物品,在体积不超过F的情况下,使得选出的物品的重量的中位数最大.所谓中位数,就是排序 ...

  2. 如何在ubuntu16上安装docker

    自从用了docker,就一直无法忘怀,省去了很多部署成本.特别是可以统一开发环境和部署环境,在实际开发中有很大的实用价值. 作为一个伪全栈,我是力推大家学习docker技术的.这种共享linux内核的 ...

  3. ArcEngine 9.3与64位操作系统 冲突

    ArcEngine 9.3与64位操作系统 冲突 2011年03月30日 星期三 11:13 错误信息: 未处理 System.TypeInitializationException  Message ...

  4. DevExpress v17.2新版亮点—WPF篇(二)

    用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v17.2 新的Hamburger Menu.Sched ...

  5. BeanFactory工厂

    Core模块主要的功能是实现了反向控制(Inversion of Control)与依赖注入(Denpendency Injection).Bean配置以及加载. Beans为Spring里的各种对象 ...

  6. Individual work 总结

    不得不说,这是我上大学以来所花时间最长.收获最多的个人项目之一.在此之前,虽然也上过面向对象等课程,课程对编程代码量的要求并不比这个小,但是由于从没有如这次这般,完全靠自己学习新的编程语言并进行编程实 ...

  7. apsx 页面 if(!ispostback)其用法和作用 什么时候该用?

    一个页面第一次显示的时候 isPostBack=false 然后你在这个页面上点击按钮或其它东西提交的时候, isPostBack=true 一般这个函数里面的内容是指第一次打开这个页面的时候要做的事 ...

  8. HDU 1518 Square(DFS)

    Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...

  9. Android中的一些简单的adb命令

    外设为手机.也配置好了Android的adb环境变量,就可以执行了.

  10. 51Nod:1268 和为K的组合

    1268 和为K的组合  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以 ...