LocalDateTime、LocalDate与Date互转替换 SimpleDateFormat

SimpleDateFormat 的format, parse 方法线程不安全的 坑呀 !!!!

[ 例 ]

static SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
static SimpleDateFormat sHourFormat = new SimpleDateFormat("yyyyMMddHH");
static SimpleDateFormat sFullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/** 将字符串类型的日期(yyyy-MM-dd)转换成Date 2 **/
public static Date parse2Date(String pDate) {
   try {
      return sFullFormat.parse(pDate);
   } catch (ParseException ex) {
      return null;
   }
}

/** 将字符串类型的日期(yyyy-MM-dd)转换成Date* 1 */
public static Date parse2Date2(String pDate) {
   try {
      return sDateFormat.parse(pDate);
   } catch (ParseException ex) {
      return null;
   }
}

start run job,eid:13240939,datekey:2023-05-22,initTime:2023-05-22 00:15:00,datekey2:Sun Sep 22 00:00:00 CST 2024,initTime2:Mon May 22 00:15:00 CST 2023

2023-05-22 00:15:02,481  INFO [defaultJobExecutor-thread-280855] (ShellJob.java:80) - shell----创建shell脚本开始:/var/lib/hadoop-hdfs/workpath/script/9733_6f63c4cc-1b64-4506-80a7-8bfeea74cf43.sh,0,2

2023-05-22 00:15:02,481  INFO [Thread-27] (WorkerThriftImpl.java:54) - start run job,eid:13240939,datekey:2023-05-22,initTime:2023-05-22 00:15:00,datekey2:Sun Sep 22 00:00:00 CST 2024,initTime2:Mon May 22 00:15:00 CST 2023

其中 datekey 置换前:2023-05-22 ,置换后 datekey2:Sun Sep 22 00:00:00 CST 2024

解决思路 如下:

一、LocalDateTime转Date

package com.xccc;
import java.time.ZoneId;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Calendar;
 
public class Test{
 
public static void main(String[] args)
    // LocalDateTime 转 Date
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println("local:"+localDateTime.getYear());
    ZoneId zoneId = ZoneId.systemDefault();
    Date date = Date.from(localDateTime.atZone(zoneId).toInstant());
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    System.out.println("date:"+calendar.get(Calendar.YEAR));
}

二、LocalDate转Date

package com.test;
import java.time.ZoneId;
import java.time.LocalDate;
import java.util.Date;
import java.util.Calendar;
 
public class Test{
 
public static void main(String[] args)
    // LocalDate 转 Date
    LocalDate localDate = LocalDate.now();
    System.out.println("local:"+localDate.getYear());
    ZoneId zoneId = ZoneId.systemDefault();
    Date date = Date.from(localDate.atStartOfDay().atZone(zoneId).toInstant());
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    System.out.println("date:"+calendar.get(Calendar.YEAR));
}

三、Date转LocalDate、LocalDateTime

package com.xxx;
import java.time.ZoneId;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Calendar;
 
public class Test{
 
public static void main(String[] args){
    Date date = new Date();
    // Date 转 LocalDate
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    LocalDate localDate = LocalDate.of(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(DAY_OF_MONTH));
    // Date 转 LocalDateTime 
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());
    }
}

参考:

java LocalDateTime与Date日期与字符串互转

你可能感兴趣的:(java,hadoop,开发语言)