DateUtil-工具类
/**
* 类描述:时间操作定义类
*/
public class DateUtils{
private static final Logger logger = Logger.getLogger(DateUtils.class);
// ticks时间格式
//ticksToDatetime
public static Date ticksToDate(long ticks){
if(ticks == 0){
return null;
}
return new Date((ticks- 621355968000000000l)/10000);
}
//ticksToDatetime
public static String ticksToObject(long ticks,String pattern){
if(ticks == 0){
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date((ticks- 621355968000000000l)/10000));
} // SimpleDateFormat 线程安全配置
/** 锁对象 */
private static final Object lockObj = new Object(); /** 存放不同的日期模板格式的sdf的Map */
private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>(); /**
* 返回一个ThreadLocal的sdf,每个线程只会new一次sdf
* @param pattern
* @return
*/
private static SimpleDateFormat getSdf(final String pattern) {
ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
// 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
if (tl == null) {
synchronized (lockObj) {
tl = sdfMap.get(pattern);
if (tl == null) {
// 只有Map中还没有这个pattern的sdf才会生成新的sdf并放入map // 这里是关键,使用ThreadLocal<SimpleDateFormat>替代原来直接new SimpleDateFormat
tl = new ThreadLocal<SimpleDateFormat>() { @Override
protected SimpleDateFormat initialValue() { return new SimpleDateFormat(pattern);
}
};
sdfMap.put(pattern, tl);
}
}
}
return tl.get();
}
/**
* 是用ThreadLocal<SimpleDateFormat>来获取SimpleDateFormat,这样每个线程只会有一个SimpleDateFormat
* @param date
* @param pattern
* @return
*/
public static String format(Date date, String pattern) {
return getSdf(pattern).format(date);
}
public static Date parse(String dateStr, String pattern) throws ParseException {
return getSdf(pattern).parse(dateStr);
} /**
* 本地时间转UTC时间
* @param cron cron表达式
* @param timezone 时区
* @return
*/
public static String localToUTC(String cron , int timezone) {
logger.info("===cron==="+cron);
logger.info("===timezone==="+timezone);
String offset = "";
if(timezone<0){
offset = String.valueOf(-(timezone));
}else if(timezone>0){
offset = String.valueOf(-timezone);
}else{
return cron;
}
Date date = new Date();
String crons[] = cron.split(" ");
String time = "";
String newCron = "";
String newTime = "";
logger.info("===length==="+crons.length);
if(crons.length==5){
String hour = crons[1];
String hours [] = hour.split("-");
if(hours.length==1){
String hour0 = hours[0];
if(hours[0].indexOf("/")>-1){
hour0 = hour0.substring(0,hour0.indexOf("/"));
}
if(!isInteger(hour0)){
return cron;
}else{
if("*".equals(crons[4])){
time += getCurrentYear(date)+"-";
}else{
time += crons[4]+"-";
}
if("*".equals(crons[3])){
time += getCurrentMonth(date)+"-";
}else{
time += crons[3]+"-";
}
if("*".equals(crons[2])){
time += getCurrentDay(date)+" ";
}else{
time += crons[2]+" ";
}
time += hour0 + ":" + "00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
logger.info(time);
logger.info("UTC"+offset);
Date date2 = sdf.parse(time);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"+offset));
newTime = sdf.format(date2);
logger.info("====newTime==="+newTime);
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date3 = sdf3.parse(newTime);
if(hours[0].indexOf("/")>-1){
newCron = crons[0]+" "+numberFormat(getCurrentHour(date3))+hours[0].substring(hours[0].indexOf("/"))+" ";
}else{
newCron = crons[0]+" "+numberFormat(getCurrentHour(date3))+" ";
}
if(!"*".equals(crons[2])){
newCron += numberFormat(getCurrentDay(date3))+" ";
}else{
newCron += crons[2]+" ";
}
if(!"*".equals(crons[3])){
newCron += numberFormat(getCurrentMonth(date3))+" ";
}else{
newCron += crons[3]+" ";
}
newCron += crons[4]+" ";
logger.info("====newCron==="+newCron);
return newCron;
}catch (ParseException e){
logger.error(e);
return cron;
}
}
}else{
if(!isInteger(hours[0])){
return cron;
}else{
if("*".equals(crons[4])){
time += getCurrentYear(date)+"-";
}else{
time += crons[4]+"-";
}
if("*".equals(crons[3])){
time += getCurrentMonth(date)+"-";
}else{
time += crons[3]+"-";
}
if("*".equals(crons[2])){
time += getCurrentDay(date)+" ";
}else{
time += crons[2]+" ";
}
time += hours[0] + ":" + "00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String hour0 = "";
try {
logger.info(time);
logger.info("UTC"+offset);
Date date2 = sdf.parse(time);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"+offset));
newTime = sdf.format(date2);
logger.info("====newTime==="+newTime);
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date3 = sdf3.parse(newTime);
hour0 = getCurrentHour(date3);
logger.info("====hour0==="+hour0);
}catch (ParseException e){
logger.error(e);
return cron;
} String hour1 = hours[1];
if(hours[1].indexOf("/")>-1){
hour1 = hour1.substring(0,hour1.indexOf("/"));
}
String time2 = "";
if("*".equals(crons[4])){
time2 += getCurrentYear(date)+"-";
}else{
time2 += crons[4]+"-";
}
if("*".equals(crons[3])){
time2 += getCurrentMonth(date)+"-";
}else{
time2 += crons[3]+"-";
}
if("*".equals(crons[2])){
time2 += getCurrentDay(date)+" ";
}else{
time2 += crons[2]+" ";
}
time2 += hour1 + ":" + "00";
try {
logger.info(time2);
SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date4 = sdf4.parse(time2);
sdf4.setTimeZone(TimeZone.getTimeZone("UTC"+offset));
newTime = sdf4.format(date4);
logger.info("====newTime==="+newTime);
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date3 = sdf3.parse(newTime);
String hour2 = getCurrentHour(date3);
String newHour = "";
logger.info(hours[1]);
if(hours[1].indexOf("/")>-1){
newHour = numberFormat(hour0)+"-"+numberFormat(hour2)+hours[1].substring(hours[1].indexOf("/"));
}else{
newHour = numberFormat(hour0)+"-"+numberFormat(hour2);
}
newCron = crons[0]+" "+newHour+" ";
if(!"*".equals(crons[2])){
newCron += numberFormat(getCurrentDay(date3))+" ";
}else{
newCron += crons[2]+" ";
}
if(!"*".equals(crons[3])){
newCron += numberFormat(getCurrentMonth(date3))+" ";
}else{
newCron += crons[3]+" ";
}
newCron += crons[4]+" ";
logger.info("====newCron==="+newCron);
return newCron;
}catch (ParseException e){
logger.error(e);
return cron;
}
}
}
}else{
return cron;
}
} //去除首位0
public static String numberFormat(String number) {
int num = Integer.parseInt(number);
if(num<10){
number = number.substring(1);
}
return number;
} //获得当前年份
public static String getCurrentYear(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
return sdf.format(date);
} //获得当前月份
public static String getCurrentMonth(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("MM");
return sdf.format(date);
} //获得当前日
public static String getCurrentDay(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("dd");
return sdf.format(date);
} //获得当前时
public static String getCurrentHour(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("HH");
return sdf.format(date);
}
//两时间相减获得时分秒
public static String formatDuration(String startDate ,String endDate){
if(StringUtil.isNotEmpty(startDate) && StringUtil.isNotEmpty(endDate)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化时间
try{
Long start = sdf.parse(startDate).getTime();
Long end = sdf.parse(endDate).getTime();
Long time = end-start;
int seconds = time.intValue()/1000;
return secToTime(seconds);
}catch (ParseException e){
logger.error(e);
return "";
}
}else{
return "";
}
} // 整数(秒数)转换为时分秒
public static String secToTime(int time) {
int hour = 0;
int minute = 0;
int second = 0;
if (time <= 0)
second = 0;
else {
minute = time / 60;
if (minute < 60) {
second = time % 60;
} else {
hour = minute / 60;
minute = minute % 60;
second = time - hour * 3600 - minute * 60;
}
}
String h="",m="",s="";
if(hour!=0){
h = hour+"时";
}
if(minute!=0){
m = minute+"分";
}
if(second!=0){
s = second+"秒";
}
return h+m+s;
} }
DateUtil-工具类的更多相关文章
- DateUtil工具类
package com.autoserve.mh.common.util; import java.text.SimpleDateFormat; import java.util.Calendar ...
- 邓博泽 java最全的DateUtil工具类
package com.kld.yijie.web.util; import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java. ...
- 03-自己封装DateUtil工具类
package com.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...
- hutool的DateUtil工具类
1.0.DateUitl(日期时间) 0)坐标 <dependency> <groupId>cn.hutool</groupId> <artifactId&g ...
- hutool包的DateUtil工具类
[首先引入依赖 ] <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core& ...
- JAVA DateUtil 工具类封装(转)
原文链接 https://blog.csdn.net/wangpeng047/article/details/8295623 作者三次整理后的代码 下载链接 https://www.lanzou ...
- JAVA 日期格式工具类DateUtil.java
DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...
- 日期工具类 - DateUtil.java
日期工具类,提供对日期的格式化和转换方法.获取区间日期.指定日期.每月最后一天等. 源码如下:(点击下载 -DateUtil.java.commons-lang-2.6.jar ) import ja ...
- 使用日期工具类:DateUtil
利用java开发,避免不了String.Date转换,前一天.后一天等问题.给出一个工具类,仅供学习交流. import java.text.DateFormat; import java.text. ...
- android 工具类 DateUtil
提取了一些在开发过程中可能会用到的日期相关的函数作为工具类.供大家參考: /** * 日期操作工具类. * * @author shimiso */ public class DateUtil { p ...
随机推荐
- ORACLE Physical Standby DG 之fail over
SQL> select thread#, low_sequence#, high_sequence# from v$archive_gap;确认下是否存在日志间隙,发现gap现象,说明failo ...
- 迭代器遍历列表 构造方法 constructor ArrayList Vector LinkedList Array List 时间复杂度
package priceton; import java.io.IOException; import java.util.concurrent.CyclicBarrier; import java ...
- 内存地址 Memory Management
Memory Management https://docs.python.org/2/c-api/memory.html Memory management in Python involves a ...
- python-笔记(操作excel)
python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的.这 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_5_Entry键值对对象
- 35 怎么优化join
35 怎么优化join 上一篇介绍了join的两种算法:nlj和bnl create table t1(id int primary key, a int, b int, index(a)); cre ...
- 浅谈Java反射机制 之 使用类的 属性、方法和构造函数
前面两篇我们总结了Java反射机制如何获取类的字节码,如何获取构造函数,属性和方法, 这篇我们将进一步验证如何使用我们获取到的属性.方法以及构造函数 1.使用 反射 获取到的 属性 import ja ...
- gradle阿里云镜像配置
Maven镜像的配置参考: http://blog.java1234.com/blog/articles/252.html buildscript { repositories { mavenLoca ...
- react 之 flux
[WangQi]---flux---[react] 一.什么是Flux Flux 是一种架构思想,专门解决软件的结构问题.它跟MVC 架构是同一类东西,但是更加简单和清晰. 二.flux的基本概念 ...
- css的继承之width属性(容易忽略)
众所周知,css的三大特性分别是 继承性,层叠性,和优先级. 那么这里就详细说一下css中width的继承性及其特殊情况. 继承性概念详解:css的继承性指的被包在内部的标签拥有外部标签的样式性,子元 ...