Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Safe conversion method of SimpleDateFormat Class in Java multithreaded Environment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report