/**
* 类描述:时间操作定义类
*/
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-工具类的更多相关文章

  1. DateUtil工具类

    package com.autoserve.mh.common.util;   import java.text.SimpleDateFormat; import java.util.Calendar ...

  2. 邓博泽 java最全的DateUtil工具类

    package com.kld.yijie.web.util; import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java. ...

  3. 03-自己封装DateUtil工具类

    package com.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...

  4. hutool的DateUtil工具类

    1.0.DateUitl(日期时间) 0)坐标 <dependency> <groupId>cn.hutool</groupId> <artifactId&g ...

  5. hutool包的DateUtil工具类

    [首先引入依赖 ] <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core& ...

  6. JAVA DateUtil 工具类封装(转)

    原文链接 https://blog.csdn.net/wangpeng047/article/details/8295623  作者三次整理后的代码 下载链接   https://www.lanzou ...

  7. JAVA 日期格式工具类DateUtil.java

    DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...

  8. 日期工具类 - DateUtil.java

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

  9. 使用日期工具类:DateUtil

    利用java开发,避免不了String.Date转换,前一天.后一天等问题.给出一个工具类,仅供学习交流. import java.text.DateFormat; import java.text. ...

  10. android 工具类 DateUtil

    提取了一些在开发过程中可能会用到的日期相关的函数作为工具类.供大家參考: /** * 日期操作工具类. * * @author shimiso */ public class DateUtil { p ...

随机推荐

  1. Java数据结构之单链表

    这篇文章主要讲解了通过java实现单链表的操作,一般我们开始学习链表的时候,都是使用C语言,C语言中我们可以通过结构体来定义节点,但是在Java中,我们没有结构体,我们使用的是通过类来定义我们所需要的 ...

  2. 基于python实现自动化办公学习笔记四

    PPT(1)写PPT import win32comimport win32com.client def makeppt(path): ppt = win32com.client.Dispatch(& ...

  3. sublime 3 安装格式化JSON插件

    转自 https://blog.csdn.net/sweettool/article/details/72677784     一.首先下载SublimePrettyJson插件包 https://g ...

  4. centos修改时区,同步时间

    查看当前系统时区 ls -la /etc/localtime 查看支持的时区 timedatectl list-timezones # 查看所有时区 timedatectl list-timezone ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_1_File类的概述

  6. C#模块初始化注入

    这个功能可以实现很多很有用的功能,比如程序集加密,Hook安装等.英文转载备忘.   原地址:https://www.coengoedegebure.com/module-initializers-i ...

  7. 工具 - VNC

    安装 ubuntu下vnc客户端的安装命令sudo apt-get install xtightvncviewer 重置密码 cd /root/.vnc/ rm -rf passwd vncserve ...

  8. git.ZC_命令积累

    1.删除文件 git rm 想要删除的文件的名字及其后缀 git commit -m "对本次提交的描述信息" git push 删除文件夹,执行命令: git rm 想要删除的文 ...

  9. springboot启动时报错 错误: 找不到或无法加载主类 com.xxx.xxx.Application

    1. Q1 错误: 找不到或无法加载主类 com.xxx.xxx.Application 解决办法:啥也不动,maven clean下,重启 1. Q2 layui控制下拉框高度 解决 .layui- ...

  10. 事件驱动体系结构(EDA)