Skip to content
Advertisement

Generate a current datestamp in Java

What is the best way to generate a current datestamp in Java?

YYYY-MM-DD:hh-mm-ss

Advertisement

Answer

Using the standard JDK, you will want to use java.text.SimpleDateFormat

JavaScript

However, if you have the option to use the Apache Commons Lang package, you can use org.apache.commons.lang.time.FastDateFormat

JavaScript

FastDateFormat has the benefit of being thread safe, so you can use a single instance throughout your application. It is strictly for formatting dates and does not support parsing like SimpleDateFormat does in the following example:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement