Skip to content

Instantly share code, notes, and snippets.

@andreluisdias
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreluisdias/10490557 to your computer and use it in GitHub Desktop.
Save andreluisdias/10490557 to your computer and use it in GitHub Desktop.
Concurrent Simple Date Format Example
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Andre Luis de Oliveira Dias
* @date 11/04/2014
* http://www.javacodegeeks.com/2010/07/java-best-practices-dateformat-in.html
*/
public class ConcurrentSimpleDateFormat {
private static final Logger logger = LoggerFactory.getLogger(ConcurrentSimpleDateFormat.class);
private static final ThreadLocal<SimpleDateFormat> df = new ThreadLocal<SimpleDateFormat> () {
@Override
public SimpleDateFormat get() {
return super.get();
}
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
@Override
public void remove() {
super.remove();
}
@Override
public void set(SimpleDateFormat value) {
super.set(value);
}
};
/**
*
* @param dateString
* @param formatPattern
* @return
* @throws ParseException
*/
public static Date parse(String dateString, String formatPattern) {
return parse(dateString, formatPattern, false);
}
/**
*
* @param dateString
* @param formatPattern
* @return
* @throws ParseException
*/
public static Date parseWithException(String dateString, String formatPattern) {
return parse(dateString, formatPattern, true);
}
/**
*
* @param dateString
* @param formatPattern
* @param withExceptionDetails
* @return
*/
private static Date parse(String dateString, String formatPattern, boolean withExceptionDetails) {
if (dateString != null) {
try {
SimpleDateFormat syncSdf = df.get();
syncSdf.setLenient(false); // http://www.accordess.com/wpblog/techtip-use-of-setlenient-method-on-simpledateformat/
syncSdf.applyPattern(formatPattern);
return syncSdf.parse(dateString);
} catch (Exception e) {
logger.error("Error during date parsing with pattern: " + formatPattern + " and date: " + dateString + ". Reason: " + e.getMessage());
if (withExceptionDetails) {
throw new RuntimeException(e);
}
}
}
return null;
}
/**
*
* @param date
* @param formatPattern
* @return
* @throws ParseException
*/
public static String format(Date date, String formatPattern) {
return format(date, formatPattern, false);
}
/**
*
* @param date
* @param formatPattern
* @return
* @throws ParseException
*/
public static String formatWithException(Date date, String formatPattern) {
return format(date, formatPattern, true);
}
/**
*
* @param date
* @param formatPattern
* @param withExceptionDetails
* @return
*/
private static String format(Date date, String formatPattern, boolean withExceptionDetails) {
if (date != null) {
try {
SimpleDateFormat syncSdf = df.get();
syncSdf.setLenient(false); // http://www.accordess.com/wpblog/techtip-use-of-setlenient-method-on-simpledateformat/
syncSdf.applyPattern(formatPattern);
return syncSdf.format(date);
} catch (Exception e) {
logger.error("Error during date formatting with pattern: " + formatPattern + " and date: " + date + ". Reason: " + e.getMessage());
if (withExceptionDetails) {
throw new RuntimeException(e);
}
}
}
return null;
}
public static void main (String[] args) throws Exception {
// EXAMPLES
final String[] patterns = new String[] {"dd/MM/yyyy", "dd-MM-yyyy", "yyyy-MM-dd"};
for (int i = 0; i < 1000; i++) {
for (int x = 0; x < 3; x++) {
logger.info("Formatting with " + patterns[x] + " = " + ConcurrentSimpleDateFormat.format(new Date(), patterns[x]));
}
}
}
}
@andreluisdias
Copy link
Author

Utility class for concurrent usage of java.text.SimpleDateFormat, with exception detail handling.

@andreluisdias
Copy link
Author

Lenient added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment