/**
* 类描述:时间操作定义类
*/
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;
}



}
posted on 2018-08-10 13:38  FeeCy  阅读(133)  评论(0编辑  收藏  举报