package com.sicdt.library.core.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.lang3.StringUtils; /**
*
* <br>类 名: DateTimeUtil
* <br>描 述: 日期时间工具类
* <br>作 者: shizhenwei
* <br>创 建: 2017年5月15日
* <br>版 本: v0.0.2
* <br>
* <br>历 史: (版本) 作者 时间 注释
*/
public class DateTimeUtil {
public static final String DIVIDE_LINE = "-";
public static final String DIVIDE_COLON = ":"; public static final long SECOND = 1000;
public static final long MINUTE = 60 * SECOND;
public static final long HOUR = 60 * MINUTE;
public static final long DAY = 24 * HOUR;
public static final long WEEK = 7 * DAY;
public static final String YEARFORMAT = "yyyy";
public static final String MONTHFORMAT = "yyyy-MM";
public static final String SHORTFORMAT = "yyyy-MM-dd";
public static final String SHORTFORMATNOSPIT = "yyyyMMdd";
public static final String LONGFORMATNOSPIT = "yyyyMMddHHmmss";
public static final String LONGFORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String SUPERLONGFORMAT = "yyyy-MM-dd HH:mm:ss sss";
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static final int FIELD_YEAR = Calendar.YEAR;
public static final int FIELD_MONTH = Calendar.MONTH;
public static final int FIELD_DATE = Calendar.DATE; /**
* 时间戳格式化 yyyy-MM-dd HH:mm:ss
*/
public static String getTimestampString(java.util.Date date) {
return sdf.format(date).toString();
} /**
* 系统当前时间戳
*/
public static Timestamp getTimestamp(){
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMAT);
long stamp = System.currentTimeMillis();
String timestamp = myFormat.format(stamp);
return Timestamp.valueOf(timestamp);
} /**
* 日期转时间戳
*/
public static Timestamp getTimestamp(java.util.Date date) {
return Timestamp.valueOf(getTimestampString(date));
} /**
* 字符串转时间戳
*/
public static Timestamp getTimestamp(String timestamp) {
if(StringUtils.isEmpty(timestamp)){
return null;
}
return Timestamp.valueOf(timestamp);
} /**
* 时间格式化 yyyy-MM-dd
*/
public static String getDate() {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(SHORTFORMAT);
return myFormat.format(System.currentTimeMillis());
} /**
* 时间格式化 yyyy-MM-dd
*/
public static String getDate(java.util.Date date) {
return getDate(date, SHORTFORMAT);
} /**
* 时间格式化 yyyy-MM-dd
*/
public static String getDate(java.util.Date date, String format) {
String result = null;
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(format);
result = myFormat.format(date);
} catch (Exception e) {
return null;
}
return result;
} /**
* 时间格式化 yyyy-MM-dd HH:mm:ss
* */
public static String getDateTime() {
return getDateTime(new java.util.Date());
} /**
* 时间格式化 yyyy-MM-dd HH:mm:ss
* */
public static String getDateTime(java.util.Date date) {
java.text.SimpleDateFormat d = new java.text.SimpleDateFormat(LONGFORMAT);
return d.format(date);
} /**
* 时间格式化 yyyy-MM-dd
* */
public static String getShortDate(java.util.Date date){
if(date==null)
date = new java.util.Date();
return getDate(date, SHORTFORMAT);
} /**
* 时间格式化 yyyy-MM-dd HH:mm:ss
* */
public static String getLongDate(java.util.Date date){
if(date==null)
date = new java.util.Date();
return getDateTime(date);
} /**
* 时间格式化 yyyy-MM-dd HH:mm:ss
* */
public static String getTimestampStr(){
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMAT);
return myFormat.format(System.currentTimeMillis());
} /**
* 时间格式化 yyyyMMddHHmmss
* */
public static String getLongTime(){
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMATNOSPIT);
return myFormat.format(System.currentTimeMillis());
} /**
* 时间格式化 yyyyMMddHHmmss
* */
public static String getLongTime(long ts){
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMATNOSPIT);
return myFormat.format(ts);
} /**
* 时间戳TS
*/
public static String getTS(){
long ts = System.currentTimeMillis();
return Long.toString(ts);
} /**
* N年后
* */
public static java.util.Date addYear(java.util.Date date, int amount) {
return add(date, Calendar.YEAR, amount);
} /**
* N个月后
* */
public static java.util.Date addMonth(java.util.Date date, int amount) {
return add(date, Calendar.MONTH, amount);
} /**
* N天后
* */
public static java.util.Date addDay(java.util.Date date, int amount) {
return add(date, Calendar.DATE, amount);
} /**
* N(年、月、天)后
* */
public static java.util.Date add(java.util.Date date, int field, int amount) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount);
return calendar.getTime();
} /**
* N(年、月、天)后
* */
public static java.util.Date add(String datestr, int field, int amount) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatDate(datestr));
calendar.add(field, amount);
return calendar.getTime();
} /**
* N(年、月、天)后(字符串形式)
* */
public static String add(String datestr, int field, int amount, String format) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatDate(datestr,format));
calendar.add(field, amount);
return getDate(calendar.getTime(), format);
} /***
* 获取当前时间,不含日期
* @return
*/
public static String getCurrentTime(){
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss");
String time = sdf.format(Calendar.getInstance().getTime());
return time;
} /**
* 将字符串转为为日期
*/
public static java.util.Date formatDate(String datestr) {
if(19==datestr.length()){
return formatDate(datestr, LONGFORMAT);
}else{
return formatDate(datestr, SHORTFORMAT);
}
} /**
* 将字符串转为为日期
*/
public static java.util.Date formatDate(String datestr, String format) {
try {
if(StringUtils.isEmpty(datestr)){
return null;
}
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(format);
Date date = myFormat.parse(datestr);
return date;
} catch (Exception e) {
return null;
}
} /***
* 月份的开始结束日期
* @param year
* @param month
* @param keys
* @return
*/
public static Map<String, String> getMonthStartDateAndEndDate(int year, int month,
String... keys) {
SimpleDateFormat d = new SimpleDateFormat(LONGFORMAT);
Map<String, String> date = new HashMap<String, String>();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
date.put(keys[0], d.format(calendar.getTime()));
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date.put(keys[1], d.format(calendar.getTime()));
return date;
} /**
* 时间格式化
*/
public static String getTimeString(String time) {
String[] ti = time.split(":");
if (ti[1].length() == 1) {
time = ti[0] + "0" + ti[1];
} else {
time = ti[0] + ti[1];
}
return time;
} /**
* <br>描 述: 获取计费模式的年月 2017-06-26 至 2017-07-25 为 2017年07月 以此类推
* <br>作 者: shizhenwei
* <br>历 史: (版本) 作者 时间 注释
* @param 2017-06-19/2016-06-19 15:35:35
* @return
*/
public static String getSicdtYearMonth(String datestr){
String[] fileds = datestr.split(DIVIDE_LINE);
if(Integer.valueOf(fileds[2])>25){
Date date = add(datestr, FIELD_MONTH, 1);
return getDate(date, MONTHFORMAT);
}
return datestr.substring(0, 7);
} public static String getSicdtYearMonth(){
return getSicdtYearMonth(getDate());
} @SuppressWarnings("deprecation")
public static String timeString() {
Date date = new Date();
return getTimeString(date.getHours() + ":" + date.getMinutes());
} /**
* 月-天数
* */
public static int getDayNum(int year, int month) {
if (month == 2) {
return year % 400 != 0 && (year % 4 != 0 || year % 100 == 0) ? 28
: 29;
}
String SmallMonth = ",4,6,9,11,";
return SmallMonth.indexOf(String.valueOf(String
.valueOf((new StringBuffer(",")).append(String.valueOf(month))
.append(",")))) < 0 ? 31 : 30;
}
/***
* 两个日期之间的天数
* @param date1
* @param date2
* @return
*/
public static int DateDiff(Date date1, Date date2) {
int i = (int) ((date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);
return i;
}
/**
* 日期拆分,取年或月或日 Y-年 M-月 D-日
* @param strDate
* @param style
* @return
*/
public static int getYearMonthDate(String strDate, String style) {
int year;
int month;
int day;
int firstDash;
int secondDash;
if (strDate == null) {
return 0;
}
firstDash = strDate.indexOf('-');
secondDash = strDate.indexOf('-', firstDash + 1);
if ((firstDash > 0) & (secondDash > 0)
& (secondDash < strDate.length() - 1)) {
year = Integer.parseInt(strDate.substring(0, firstDash));
month = Integer.parseInt(strDate.substring(firstDash + 1,
secondDash));
day = Integer.parseInt(strDate.substring(secondDash + 1));
} else {
return 0;
}
if (style.equalsIgnoreCase("Y")) {
return year;
} else if (style.equalsIgnoreCase("M")) {
return month;
} else if (style.equalsIgnoreCase("D")) {
return day;
} else {
return 0;
}
} /***
* 是否是年份的第一天
* @param date
* @return
*/
public static boolean isYearFirstDay(java.sql.Date date) {
boolean i = false;
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 1)
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 1)) {
i = true;
}
return i;
}
/**
* 是否是半年的第一天
* @param date
* @return
*/
public static boolean isHalfYearFirstDay(java.sql.Date date) {
boolean i = false;
if (((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 1) && (DateTimeUtil
.getYearMonthDate(date.toString(), "D") == 1))
|| ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 7) && (DateTimeUtil
.getYearMonthDate(date.toString(), "D") == 1))) {
i = true;
}
return i;
}
/**
* 取半年的第一天
* @param date
* @return
*/
public static String getHalfYearFirstDay(java.sql.Date date) {
String month = "01";
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
month = "07";
}
String day = Integer.toString(DateTimeUtil.getYearMonthDate(
date.toString(), "Y")) + "-" + month + "-01";
return day;
}
/**
* 是否是半年最后一天
* */
public static boolean isHalfYearLastDay(java.sql.Date date) {
boolean i = false;
if (((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 12) && (DateTimeUtil
.getYearMonthDate(date.toString(), "D") == 31))
|| ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 6) && (DateTimeUtil
.getYearMonthDate(date.toString(), "D") == 30))) {
i = true;
}
return i;
}
/***
* 取半年最后一天
* @param date
* @return
*/
public static String getHalfYearLastDay(java.sql.Date date) {
String month = "-06-30";
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
month = "-12-31";
}
String day = Integer.toString(getYearMonthDate(date.toString(), "Y"))
+ "-" + month;
return day;
} /***
*是否是年度最后一天
* @param date
* @return
*/
public static boolean isYearLastDay(java.sql.Date date) {
boolean i = false;
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 12)
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 31)) {
i = true;
}
return i;
} /**
* 是否季度第一天
* */
public static boolean isQuarterFirstDay(java.sql.Date date) {
boolean i = false;
if (((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 1)
|| (DateTimeUtil.getYearMonthDate(date.toString(), "M") == 4)
|| (DateTimeUtil.getYearMonthDate(date.toString(), "M") == 7) || (DateTimeUtil
.getYearMonthDate(date.toString(), "M") == 10))
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 1)) {
i = true;
}
return i;
}
/***
* 季度第一天
* @param date
* @return
*/
public static String getQuarterFirstDay(java.sql.Date date) {
String month = "01";
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 10) {
month = "10";
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
month = "07";
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 4) {
month = "04";
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 1) {
month = "01";
} String day = Integer.toString(DateTimeUtil.getYearMonthDate(
date.toString(), "Y")) + "-" + month + "-01";
return day;
} /**
* 是否季度最后一天
* @param date
* @return
*/
public static boolean isQuarterLastDay(java.sql.Date date) {
boolean i = false;
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 3)
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 31)) {
i = true;
}
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 6)
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 30)) {
i = true;
}
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 9)
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 30)) {
i = true;
}
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 12)
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 31)) {
i = true;
}
return i;
}
/**
* 季度最后一天
* @param date
* @return
*/
public static String getQuarterLastDay(java.sql.Date date) {
String month = "-03-31";
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 10) {
month = "-12-31";
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
month = "-09-30";
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 4) {
month = "-06-30";
} String day = Integer.toString(DateTimeUtil.getYearMonthDate(
date.toString(), "Y")) + "-" + month;
return day;
} /***
* 是否月份最后一天
* @param date
* @return
*/
public static boolean isMonthLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
} /**
* 是否月份第一天
* @param date
* @return
*/
public static boolean isMonthFisrtDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMinimum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
} /**
* 月份第一天日期
* @param date
* @return
*/
public static Date getMonthFisrtDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
} /**
* 星期的第几天
* @param strDate yy-mm-dd
* @return
*/
public static int getDateInWeek(String strDate) {
DateFormat df = DateFormat.getDateInstance();
try {
df.parse(strDate);
java.util.Calendar c = df.getCalendar();
int day = c.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
return day;
} catch (ParseException e) {
return -1;
}
} public static java.sql.Date getSqlDate(java.util.Date date) {
java.sql.Date result = null;
try {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMAT);
String mystrdate = myFormat.format(date);
result = java.sql.Date.valueOf(mystrdate);
} catch (Exception e) {
return null;
}
return result;
} /**
* 上个月
* @param dt
* @return
*/
@SuppressWarnings("deprecation")
public static String getUpMDate(Date dt) {
dt.setDate(1);
dt.setDate(dt.getDate() - 1);
return dt.toLocaleString();
} public static String getShortDate(String dt) {
if (dt != null) {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(SHORTFORMAT);
try {
Date date = myFormat.parse(dt);
return getDate(date, SHORTFORMATNOSPIT);
} catch (ParseException e) {
return dt;
}
} else
return dt;
} public static String getLongDate(String dt) {
if (dt != null) {
java.text.SimpleDateFormat myFormat = new SimpleDateFormat(SHORTFORMAT);
try {
Date date = myFormat.parse(dt);
return getDate(date, SHORTFORMAT);
} catch (ParseException e) {
return dt;
}
} else
return dt;
} public static boolean isSameYearMonth(String date) {
try {
String currdate = getDate();
currdate = getShortDate(currdate).substring(0, 6);
String lastdate = getShortDate(date).substring(0, 6);
if (lastdate.equals(currdate)) {
return true;
} else {
return false;
}
} catch (NumberFormatException e) {
return false;
}
}
}

java DateTimeUtil 日期工具类的更多相关文章

  1. JAVA 8 日期工具类

    JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...

  2. Java案例——日期工具类

    需求:定义一个日期工具类,包含两个方法,按日期转化成指定格式的字符串,把字符串解析为指定格式的日期 然后定义一个测试类测试 分析: 1.定义一个日期工具类 2.定义一个方法dateToString,用 ...

  3. Java的日期工具类

    package com.tech.jin.util; import java.text.DateFormat; import java.text.ParseException; import java ...

  4. java 常用日期工具类的操作

    获取指定日期的时间戳 /* * 获取指定日期的时间戳 * */ public static long getMillis(String date_str){ try { SimpleDateForma ...

  5. 日期工具类 - DateUtil.java

    日期工具类,提供对日期的格式化和转换方法.获取区间日期.指定日期.每月最后一天等. 源码如下:(点击下载 -DateUtil.java.commons-lang-2.6.jar ) import ja ...

  6. Java基础知识强化92:日期工具类的编写和测试案例

    1. DateUtil.java,代码如下: package cn.itcast_04; import java.text.ParseException; import java.text.Simpl ...

  7. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  8. java 日期工具类DateUtils

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

  9. java日期工具类DateUtil-续一

    上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...

随机推荐

  1. SHELL $RANDOM产生的随机数范围是0到32767

    1.使用系统的 $RANDOM 变量 fdipzone@ubuntu:~$ echo $RANDOM 17617 fdipzone@ubuntu:~$ echo $RANDOM 17617 $RAND ...

  2. jxta 2.8x启动了

    http://chaupal.github.io/ ———————————————————————————————————————————————————————————————————— 至少两个月 ...

  3. makefile编写---.a静态库的生成和调用

    #.SUFFIXES: .c .o Cc =gcc #OSA=/data/users/osa IncDir=-I. -I./ Debug = -g Cflags = -c $(DEBUG) Libs ...

  4. python3----字符串中的字符倒转

    方法一,使用[::-1]: s = 'python' print(s[::-1]) 方法二,使用reverse()方法: n = list(s) n.reverse() print(''.join(n ...

  5. js事件绑定的方法

    废话不多少,直接上代码 第一种 <body> <div style="width:400px;height:400px;background:blueviolet" ...

  6. Android开发:fragment将事件传递回activity

    fragment触发事件后传递会给activity,可以通过在fragment中定义一个接口,让activity实现这个接口. 具体代码如下 public class AAFragment exten ...

  7. JSON Extractor/jp@gc - JSON Path Extractor 举例2

    测试描述 使用json返回结果做校验 测试步骤 1.配置http请求 2.根据结果树返回的json,取值 { "status_code":200, "message&qu ...

  8. Eclipse 启动时闪退问题解决方案

    一.以前Eclipse都可以正常使用,突然有一天不能启动了,点击图标后启动画面一闪之后就消失了,以下是一些解决方案: 1. 找到Eclipse目录下的eclipse.exe,右键点击->发送到桌 ...

  9. 【BZOJ2506】calc 分段+vector+莫队

    [BZOJ2506]calc Description          给一个长度为n的非负整数序列A1,A2,…,An.现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且A ...

  10. L - Sum It Up(DFS)

    L - Sum It Up Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Descr ...