整理的java的日期DateUtil
package cn.knet.data.untils; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; /**
* yyyy-MM-dd HH:mm:ss
* @author zhaoyb
*
*/
public class DateUtil { /**
* 把日期字符串格式化成日期类型
* @param dateStr
* @param format
* @return
*/
public static Date convert2Date(String dateStr, String format) {
SimpleDateFormat simple = new SimpleDateFormat(format);
try {
simple.setLenient(false);
return simple.parse(dateStr);
} catch (Exception e) {
return null;
}
} /**
* 把日期类型格式化成字符串
* @param date
* @param format
* @return
*/
public static String convert2String(Date date, String format) {
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
return formater.format(date);
} catch (Exception e) {
return null;
}
} /**
* 转sql的time格式
* @param date
* @return
*/
public static java.sql.Timestamp convertSqlTime(Date date){
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
return timestamp;
} /**
* 转sql的日期格式
* @param date
* @return
*/
public static java.sql.Date convertSqlDate(Date date){
java.sql.Date Datetamp = new java.sql.Date(date.getTime());
return Datetamp;
} /**
* 获取当前日期
* @param format
* @return
*/
public static String getCurrentDate(String format) {
return new SimpleDateFormat(format).format(new Date());
} /**
* 获取时间戳
* @return
*/
public static long getTimestamp()
{
return System.currentTimeMillis();
} /**
* 获取月份的天数
* @param year
* @param month
* @return
*/
public static int getDaysOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
} /**
* 获取日期的年
* @param date
* @return
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
} /**
* 获取日期的月
* @param date
* @return
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
} /**
* 获取日期的日
* @param date
* @return
*/
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
} /**
* 获取日期的时
* @param date
* @return
*/
public static int getHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR);
} /**
* 获取日期的分种
* @param date
* @return
*/
public static int getMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
} /**
* 获取日期的秒
* @param date
* @return
*/
public static int getSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
} /**
* 获取星期几
* @param date
* @return
*/
public static int getWeekDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
return dayOfWeek-1;
} /**
* 获取哪一年共有多少周
* @param year
* @return
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekNumOfYear(c.getTime());
} /**
* 取得某天是一年中的多少周
* @param date
* @return
*/
public static int getWeekNumOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
} /**
* 取得某天所在周的第一天
* @param year
* @return
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c.getTime();
} /**
* 取得某天所在周的最后一天
* @param date
* @return
*/
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
return c.getTime();
} /**
* 取得某年某周的第一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
* @param year
* @param week
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
Calendar calFirst = Calendar.getInstance();
calFirst.set(year, 0, 7);
Date firstDate = getFirstDayOfWeek(calFirst.getTime()); Calendar firstDateCal = Calendar.getInstance();
firstDateCal.setTime(firstDate); Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE)); Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, (week - 1) * 7);
firstDate = getFirstDayOfWeek(cal.getTime()); return firstDate;
} /**
* 取得某年某周的最后一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周, 2009-01-04为
* 2008年最后一周的最后一天
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
Calendar calLast = Calendar.getInstance();
calLast.set(year, 0, 7);
Date firstDate = getLastDayOfWeek(calLast.getTime()); Calendar firstDateCal = Calendar.getInstance();
firstDateCal.setTime(firstDate); Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE)); Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, (week - 1) * 7);
Date lastDate = getLastDayOfWeek(cal.getTime()); return lastDate;
} private static Date add(Date date, int calendarField, int amount) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
} else {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
} /*
* 1则代表的是对年份操作, 2是对月份操作, 3是对星期操作, 5是对日期操作, 11是对小时操作, 12是对分钟操作, 13是对秒操作,
* 14是对毫秒操作
*/ /**
* 增加年
* @param date
* @param amount
* @return
*/
public static Date addYears(Date date, int amount) {
return add(date, 1, amount);
} /**
* 增加月
* @param date
* @param amount
* @return
*/
public static Date addMonths(Date date, int amount) {
return add(date, 2, amount);
} /**
* 增加周
* @param date
* @param amount
* @return
*/
public static Date addWeeks(Date date, int amount) {
return add(date, 3, amount);
} /**
* 增加天
* @param date
* @param amount
* @return
*/
public static Date addDays(Date date, int amount) {
return add(date, 5, amount);
} /**
* 增加时
* @param date
* @param amount
* @return
*/
public static Date addHours(Date date, int amount) {
return add(date, 11, amount);
} /**
* 增加分
* @param date
* @param amount
* @return
*/
public static Date addMinutes(Date date, int amount) {
return add(date, 12, amount);
} /**
* 增加秒
* @param date
* @param amount
* @return
*/
public static Date addSeconds(Date date, int amount) {
return add(date, 13, amount);
} /**
* 增加毫秒
* @param date
* @param amount
* @return
*/
public static Date addMilliseconds(Date date, int amount) {
return add(date, 14, amount);
} /**
* time差
* @param before
* @param after
* @return
*/
public static long diffTimes(Date before, Date after){
return after.getTime() - before.getTime();
} /**
* 秒差
* @param before
* @param after
* @return
*/
public static long diffSecond(Date before, Date after){
return (after.getTime() - before.getTime())/1000;
} /**
* 分种差
* @param before
* @param after
* @return
*/
public static int diffMinute(Date before, Date after){
return (int)(after.getTime() - before.getTime())/1000/60;
} /**
* 时差
* @param before
* @param after
* @return
*/
public static int diffHour(Date before, Date after){
return (int)(after.getTime() - before.getTime())/1000/60/60;
} /**
* 天数差
* @param date1
* @param date2
* @return
*/
public static int diffDay(Date before, Date after) {
return Integer.parseInt(String.valueOf(((after.getTime() - before.getTime()) / 86400000)));
} /**
* 月差
* @param before
* @param after
* @return
*/
public static int diffMonth(Date before, Date after){
int monthAll=0;
int yearsX = diffYear(before,after);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(before);
c2.setTime(after);
int monthsX = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
monthAll=yearsX*12+monthsX;
int daysX =c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
if(daysX>0){
monthAll=monthAll+1;
}
return monthAll;
} /**
* 年差
* @param before
* @param after
* @return
*/
public static int diffYear(Date before, Date after) {
return getYear(after) - getYear(before);
} /**
* 设置23:59:59
* @param date
* @return
*/
public static Date setEndDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
} /**
* 设置00:00:00
* @param date
* @return
*/
public static Date setStartDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
return calendar.getTime();
} }
整理的java的日期DateUtil的更多相关文章
- [转载]Java 8 日期&时间 API
Java 8 日期和时间 声明 本文转自http://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant,以mark ...
- 国外程序员整理的Java资源大全分享
Java 几乎是许多程序员们的入门语言,并且也是世界上非常流行的编程语言.国外程序员 Andreas Kull 在其 Github 上整理了非常优秀的 Java 开发资源,推荐给大家. 译文由 Imp ...
- 【转】国外程序员整理的Java资源大全
Java几乎是许多程序员们的入门语言,并且也是世界上非常流行的编程语言.国外程序员Andreas Kull在其Github上整理了非常优秀的Java开发资源,推荐给大家.译文由ImportNew- 唐 ...
- JAVA 8 日期工具类
JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...
- Java常用日期操作
对java中常用的日期操作进行整理. 1.日期格式化 /* * 日期格式化类(必须掌握) * API: * G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Mo ...
- 【转载】国外程序员整理的Java资源大全
以下转载自: 推荐!国外程序员整理的Java资源大全中文版 https://github.com/akullpp/awesome-java英文版 Java 几乎是许多程序员们的入门语言,并且也是 ...
- 【转】JAVA 8 日期/时间(Date Time)API指南
前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...
- 记一个JAVA关于日期的坑
JAVA解析日期格式代码,之前一直写成:“yyyy-MM-dd hh:mm”,比如"2016-01-18 11:00"."2016-01-18 15:00"都可 ...
- 国外程序员整理的Java资源
好资料,慢慢学习.http://www.importnew.com/14429.html 构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理, ...
随机推荐
- JavaScript函数小结
JS基础知识 /********************** 1:基础知识 1 创建脚本块 1: <script language=”JavaScript”> 2: JavaScript ...
- 转NodeJS的npm模块版本号 模式解析
npm 中的模块版本都需要遵循 semver 2.0 的语义化版本规则. 版本格式:主版本号.次版本号.修订号,版本号递增规则如下: 主版本号:当你做了不兼容的API 修改, 次版本号:当你做了向下兼 ...
- 设计模式:享元模式(Flyweight)
定 义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...
- ArcSDE for SQL Server安装及在ArcMap中创建ArcSDE连接
原文:ArcSDE for SQL Server安装及在ArcMap中创建ArcSDE连接 安装ArcSDE for SQL Server,最后一步成功后的界面如下: 在ArcMap中创建ArcSDE ...
- PySe-001-基础环境配置(MacOX)
Python 是一种面向对象.解释型计算机程序设计语言,其源代码同样遵循 GPL(GNU General Public License)协议.Python语法简洁而清晰,具有丰富和强大的类库.由于Py ...
- Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题
日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...
- Java学习-011-创建文件实例及源代码
此文源码主要为应用 Java 创建文件的源代码.若有不足之处,敬请大神指正,不胜感激! 创建文件的源代码如下所示: /** * @function 文件操作:创建文件.若文件不存在,则直接创建文件:若 ...
- iOS8 推送注册方式改变的问题
不久之后iPhone 6/6 plus就会在国内如雨后春笋般遍地开花了.iOS 8早已现行一步,不过有的开发者也注意到了在iOS 8上推送通知的注册方式有所变化,报错提示为: 1 registerFo ...
- disable jboss JMXInvokerServlet .
jboss 默认有几个控制台,都是可能存在漏洞被黑客利用,除了web console .jmx console. 还有JMXInvokerServlet,访问路径是ip/invoker/JMXInvo ...
- javascript原生dom操作方法
一.节点层次属性 考虑空白符的相关层次关系属性: 1.childNodes属性 包含 2.parentNode属性 3.previouseSibling属性和nextSibling属性 4.first ...