James Owen’s answer is correct: you need to use lowercase yyyy
and lowercase dd
in your format pattern string. That should do it.
What I want to contribute here is the modern and improved version of your utility class:
public class Util
{
public static final String DATETIME_PATTERN = "yyyy/MM/dd - HH:mm:ss";
public static String getDateTime()
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DATETIME_PATTERN);
return getNow().format(dtf);
}
public static ZonedDateTime getNow()
{
return ZonedDateTime.now(ZoneId.of("Asia/Taipei"));
}
}
Since you are using Java 8, there is no reason for you to take the trouble with the long outdated classes SimpleDateFormat
, Date
and Calendar
. The modern classes are generally much nicer to work with. I took the freedom of also returning a bit more information from getToday()
(and renamed it to getNow
): it now tells you both the current time and the time zone, which makes sure that getDateTime()
also formats the time in this time zone.
I preferred Asia/Taipei
as time zone. It gives the same result as GMT+08:00
, but (1) is futureproof in case Taiwan again introduces summer time (DST) (2) is clearer to read and tells the reader more about why this particular offset was chosen (Taiwan has had summer time before, last time was in 1979).
An example of the trouble with SimpleDateFormat
One out of many objections to the old classes is they tend to pretend everything is well when it isn’t. You got an incorrect output and no explanation why. You would have preferred some error message. The modern classes behave a bit better on this point. Say you had used your pattern string with my code. It gives
java.time.DateTimeException: Field DayOfYear cannot be printed as the
value 187 exceeds the maximum print width of 2
Day of year? You may have to check the documentation to see that capital D
is for day-of-year while small d
is for day-of-month. Let’s fix:
public static final String DATETIME_PATTERN = "YYYY/MM/dd - HH:mm:ss";
Unfortunately DateTimeFormatter
does not catch your other error, the uppercase YYYY
. This means week-based year and is only useful with a week number. But theoretically you might have intended this, the class has no chance to tell. If you had tried to parse with the same format, you would have been told you couldn’t.
If you need a Date
Maybe you need a Date
object for some legacy API that you cannot change. In Java 8 the old classes have been fitted with conversion methods, so it’s easy (when you know how). You may for example add this method:
public static Date getNowAsOldfashionedDateObject() {
return Date.from(getNow().toInstant());
}