Get the App
SLTechnology News&Howtos  ›  Development  › 

Safe conversion method of SimpleDateFormat Class in Java multithreaded Environment

Shulou Source: shulou.com Published: 2022-06-03 14:16:22 09月28日 Update

This article mainly talks about "the safe conversion method of SimpleDateFormat class in Java multithreaded environment". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the safe conversion method of SimpleDateFormat class in Java multithreaded environment"!

The SimpleDateFormat class

Package state;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * SimpleDateFormat class is responsible for date conversion and formatting * solving conversion errors in SimpleDateFormat multithreaded environment * @ author zc * * / public class SimpleDateFormatThread extends Thread {private SimpleDateFormat sdf; private String dateString; public SimpleDateFormatThread (SimpleDateFormat sdf,String dateString) {super (); this.sdf = sdf; this.dateString = dateString } @ Override public void run () {try {/ / System.out.println (dateString); / / the start date is incorrectly formatted as YYYY-MM-dd Date date = DateTools.getSimpleDateFormat ("yyyy-MM-dd") .parse (dateString); / / System.out.println (date); String newDateString = DateTools.getSimpleDateFormat ("yyyy-MM-dd") .format (date). ToString () If (! newDateString.equals (dateString)) {System.out.println ("ThreadName=" + this.getName () + "error date" + dateString+ "conversion date" + newDateString);} else {System.out.println ("ThreadName=" + this.getName () + dateString+ "conversion date" + newDateString);} catch (ParseException e) {/ / TODO Auto-generated catch block e.printStackTrace () } public static void main (String [] args) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String [] dateStringArray = new String [] {"2020-02-05", "2020-02-06", "2020-02-07", "2020-02-08"}; SimpleDateFormatThread [] threadArray= new SimpleDateFormatThread [4]; for (int item0

Tags: Thread method state date environment security content format error learning utility deeper interest name utility actual object not yet simple operation quantity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Apple vpn macOS