Java时间日期格式转换Date转String和String转Date
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class DateUtil { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /*
获取昨天的日期
*/
public static Date getYesterday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,-1);
Date yesterday = cal.getTime();
return yesterday;
} /*
获取传入时间的日份
*/
public static int getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DATE);
} /**
* 获取传入时间的月份
*/
public static int getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
} /**
* 获取传入时间的季度
*/
public static int getQuarter(Date date) {
int month = getMonth(date);
if (month <= 3)
return 1;
else if (month <= 6)
return 2;
else if (month <= 9)
return 3;
else
return 4;
} /**
* 获取传入时间的年份
*/
public static int getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
} /**
* 获取传入时间月份天数
*/
public static int getCurrentMonthDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
cal.roll(Calendar.DATE, -1);
return cal.get(Calendar.DATE);
} /**
* 获得传入时间为是星期几
*/
public static int getWeekDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int weekDay = c.get(Calendar.DAY_OF_WEEK);
return weekDay - 1;
} /*
获取所在周指定星期几的日期Date
*/
public static Date getWeekDayDate(Date date, int num) {
Calendar c = Calendar.getInstance();
c.setTime(date);
switch (num) {
case 1:
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
break;
case 2:
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
break;
case 3:
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
break;
case 4:
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
break;
case 5:
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
break;
case 6:
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
break;
case 7:
// c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
break;
}
return c.getTime();
} /*
获取传入时间所在周第一天,周一
*/
public static Date getFirstWeekDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
c.add(Calendar.DATE, -weekday);
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
} /*
获取传入时间所在周最后一天,周日
*/
public static Date getLastWeekDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
try {
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
} /**
* 根据日期获取月初第一天
*/
public static Date getFirstMonthDay(Date date) {
Calendar cal = Calendar.getInstance();// 获取当前日期
cal.setTime(date);
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
} /**
* 根据日期获取月末最后一天
*/
public static Date getLastMonthDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return cal.getTime();
} /*
根据日期获取所在季度的第一天
*/
public static Date getFirstQuarterDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int currentMonth = cal.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
cal.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
cal.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
cal.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
} catch (Exception e) {
e.printStackTrace();
}
return cal.getTime();
} /*
根据日期获取所在季度的最后一天
*/
public static Date getLastQuarterDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int currentMonth = cal.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
cal.set(Calendar.MONTH, 2);
else if (currentMonth >= 4 && currentMonth <= 6)
cal.set(Calendar.MONTH, 5);
else if (currentMonth >= 7 && currentMonth <= 9)
cal.set(Calendar.MONTH, 8);
else if (currentMonth >= 10 && currentMonth <= 12)
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
} catch (Exception e) {
e.printStackTrace();
}
return cal.getTime();
} /*
所在年前/后半年开始日期
*/
public static Date getHalfYearStartTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
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);
return c.getTime();
} /**
所在年前/后半年的结束时间
*/
public static Date getHalfYearEndTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
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);
}
return c.getTime();
} /**
* 根据日期获取年第一天
*/
public static Date getYearFirstDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year=calendar.get(Calendar.YEAR);
Calendar cd = Calendar.getInstance();
cd.clear();
cd.set(Calendar.YEAR, year);
cd.roll(Calendar.DAY_OF_YEAR,0);
Date currYearLast = cd.getTime();
return currYearLast;
} /*
获取当月指定天数日期
*/
public static Date getDateByDay(Date date, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, day);
return cal.getTime();
} /*
获取上月指定天数日期
*/
public static Date getLastMonthByDay(Date date, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
return cal.getTime();
} /**
* 获取传入时间所在季度总天数
*/
public static int getCurrentQuarterDay(Date date) {
Date firstDayDate = getFirstQuarterDay(date);
Date lastDayDate = getLastQuarterDay(date);
int day = (int) ((lastDayDate.getTime() - firstDayDate.getTime()) / (24*3600*1000) + 1);
return day;
} /*
判断日期是否为周日
*/
public static boolean isWeekEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
return true;
} else{
return false;
}
} /*
判断日期是否为月末
*/
public static boolean isMonthEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int getDay = calendar.get(Calendar.DAY_OF_MONTH);
if(getDay == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
return true;
}else{
return false;
}
} /*
判断日期是否为季末(最后一天)
*/
public static boolean isQuarterEnd(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
Date dt = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
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);
}
dt = sdf.parse(sdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
if (date.compareTo(dt) == 0) {
return true;
} else {
return false;
}
} /*
据日期获取去年年末
*/
public static Date getLastYearLastDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year=calendar.get(Calendar.YEAR)-1;
Calendar cd = Calendar.getInstance();
cd.clear();
cd.set(Calendar.YEAR, year);
cd.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = cd.getTime();
return currYearLast;
} /**
* 属于本年第几天
*/
public static int getYearDayIndex(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_YEAR);
} /**
* 属于本年第几周
*/
public static int getYearWeekIndex(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
} /*
获取两个日期间的天数,直接传入Date
*/
public static Long getDayByDates(Date startDate, Date endDate) {
Long day = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
return day;
} /*
获取两个日期间的天数,直接传入StrDate
*/
public static Long getDayByStrDates(String startStrDate, String endStrDate) throws ParseException {
Date startDate = sdf.parse(startStrDate);
Date endDate = sdf.parse(endStrDate);
Long day = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
return day;
} /**
* 判断是否闰年
* 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
* 3.能被4整除同时能被100整除则不是闰年
*/
public static boolean isLeapYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
if ((year % 400) == 0) {
return true;
} else if ((year % 4) == 0) {
if ((year % 100) == 0) {
return false;
} else {
return true;
}
} else {
return false;
}
} public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println("前一天日期为:" + sdf.format(getYesterday(date)));
System.out.println("该日期的日份:" + getDay(date));
System.out.println("该日期的月份:" + getMonth(date));
System.out.println("该日期的季度:" + getQuarter(date));
System.out.println("该日期的年份:" + getYear(date));
System.out.println("所在月总天数:" + getCurrentMonthDay(date));
System.out.println("所在季度总天数:" + getCurrentQuarterDay(date));
System.out.println("该日期为本周星期:" + getWeekDay(date));
System.out.println("所在周的星期天的日期:" + sdf.format(getWeekDayDate(date, 7)));
System.out.println("所在周第一天日期:" + sdf.format(getFirstWeekDay(date)));
System.out.println("所在周最后一天日期:" + sdf.format(getLastWeekDay(date)));
System.out.println("所在月第一天日期:" + sdf.format(getFirstMonthDay(date)));
System.out.println("所在月最后一天日期:" + sdf.format(getLastMonthDay(date)));
System.out.println("所在季度第一天日期:" + sdf.format(getFirstQuarterDay(date)));
System.out.println("所在季度最后一天日期:" + sdf.format(getLastQuarterDay(date)));
System.out.println("所在年前/后半年开始日期:" + sdf.format(getHalfYearStartTime(date)));
System.out.println("所在年前/后半年结束日期:" + sdf.format(getHalfYearEndTime(date)));
System.out.println("所在年第一天日期:" + sdf.format(getYearFirstDay(date)));
System.out.println("所在月指定天数日期:" + sdf.format(getDateByDay(date, 10)));
System.out.println("上月指定天数日期:" + sdf.format(getLastMonthByDay(date, 10)));
System.out.println("是否为周日:" + isWeekEnd(date));
System.out.println("是否为月末:" + isMonthEnd(date));
System.out.println("是否为季末:" + isQuarterEnd(date));
System.out.println("去年最后一天日期:" + sdf.format(getLastYearLastDay(date)));
System.out.println("属于本年第几天:" + getYearDayIndex(date));
System.out.println("属于本年第几周:" + getYearWeekIndex(date));
System.out.println("两个日期的间隔天数:" + getDayByDates(sdf.parse("2019-10-28"), date));
System.out.println("两个日期的间隔天数:" + getDayByStrDates("2019-10-23", "2019-10-30"));
System.out.println("是否为闰年:" + isLeapYear(date));
}
}
测试结果:
前一天日期为:2019-10-29
该日期的日份:30
该日期的月份:10
该日期的季度:4
该日期的年份:2019
所在月总天数:31
所在季度总天数:92
该日期为本周星期:3
所在周的星期天的日期:2019-11-03
所在周第一天日期:2019-10-28
所在周最后一天日期:2019-11-03
所在月第一天日期:2019-10-01
所在月最后一天日期:2019-10-31
所在季度第一天日期:2019-10-01
所在季度最后一天日期:2019-12-31
所在年前/后半年开始日期:2019-07-01
所在年前/后半年结束日期:2019-12-31
所在年第一天日期:2019-01-01
所在月指定天数日期:2019-10-10
上月指定天数日期:2019-09-10
是否为周日:false
是否为月末:false
是否为季末:false
去年最后一天日期:2018-12-31
属于本年第几天:303
属于本年第几周:44
两个日期的间隔天数:2
两个日期的间隔天数:7
是否为闰年:false
Java时间日期格式转换Date转String和String转Date的更多相关文章
- Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html
Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...
- Java时间日期格式转换
1.这个是系统自动默认的时间格式,或者说是美式格式: Long time = System.currentTimeMillis(); Date date = new Da ...
- Java练习 SDUT-2246_时间日期格式转换
时间日期格式转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于日期的常用格式,在中国常采用格式的是"年 ...
- 【dateFormatSymbols】JAVA特殊日期格式转换
记录:特殊日期格式转换,如将yyyyMMdd转为01MAY2019 public static final String DATE_VIP_FORMAT = "yyyyMMdd"; ...
- [js] js和C# 时间日期格式转换
下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...
- js和C# 时间日期格式转换
下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...
- poj 3751 时间日期格式转换
题目链接:http://poj.org/problem?id=3751 题目大意:按照要求的格式将输入的时间日期进行转化. #include <iostream> #include < ...
- Java实现时间日期格式转换示例
package com.hanqi.util; import java.text.ParseException; import java.text.SimpleDateFormat; import j ...
- java常见日期格式转换以及日期的获取
package com.test.TestBoot.SingleModel;import java.text.SimpleDateFormat;import java.util.Date;public ...
随机推荐
- LSB、MSB是什么单位
最低有效位 (LSB: Least Significant Bit) 最低有效位(LSB)是给这些单元值的一个二进制整数位位置,就是,决定是否这个数字是偶数或奇数.LSB有时候是指最右边的位,因为 ...
- 通过urllib2抓取网页内容(1)
一.urllib2发送请求 import urllib2 url = 'http://www.baidu.com' req = urllib2.Request(url) response = urll ...
- php require和include差别
require 的用法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面.PHP 程序在运行前,就会先读入 require 所 ...
- velocity简单样例
velocity简单样例整体实现须要三个步骤,详细例如以下: 1.创建一个Javaproject 2.导入须要的jar包 3.创建须要的文件 ============================= ...
- luogu2161 [SHOI2009]会场预约
题目大意 随着时间的推移这里有几个任务对应着一段区间.每次要将任务安到时间线上时,要把时间线上已有的与该任务对应区间有交集的区间对应的任务删去.求每次删去的区间个数,以及整个时间线上有几个任务.时间线 ...
- Linux I2C驱动分析(三)----i2c_dev驱动和应用层分析 【转】
本文转载自:http://blog.chinaunix.net/uid-21558711-id-3959287.html 分类: LINUX 原文地址:Linux I2C驱动分析(三)----i2c_ ...
- 3.3 文件I/O
错误的解决方法请参考:http://liangruijun.blog.51cto.com/3061169/673776 3.3.2 访问手机中的存储文件夹 3.3.3 读取assets中的文件 pac ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- Greenplum使用教程
Greenplum简介 GreenPlum是一个关系型数据库集群.,它实际上是由多个独立的数据库服务组合成的逻辑数据库.GreenPlum是基于PostgreSQL(开源数据库)的分布式数据库,它采用 ...
- 线性预测与Levinson-Durbin算法实现
在学习信号处理的时候,线性预测是一个比较难理解的知识点,为了加快很多朋友的理解,这里给出Levinson-Durbin算法的线性预测实现和一个测试Demo,Demo中很明确的把输入信号.预测信号.预测 ...