Skip to main content

Posts

Showing posts from January 17, 2019

Java Concurrency - SimpleDateFormat Is Not Simple

Greeting!

Formatting, parsing dates is a painful task. It always give us errors :(.
A common way to format, parse dates in Java is using SimpleDateFormat. Here is a common class we can use.

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public final class DateUtils { public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private DateUtils() {} public static Date parse(String target) { try { return SIMPLE_DATE_FORMAT.parse(target); } catch (ParseException e) { e.printStackTrace(); } return null; } public static String format(Date target) { return SIMPLE_DATE_FORMAT.format(target); } }
Do you think this is working. Let's test it.

private static void testSimpleDateFormatInSingleThread() { final String source = "2019-01-11"; System.out.println(DateUtils.parse(source)); …