Skip to content

DateUtils

Dokkaltek edited this page Apr 16, 2025 · 4 revisions

Date objects

Parsing dates

You can parse dates from strings easily and manipulate them as far as they are similar to the following ISO formats:

String dateWithFullTime = "2025-08-04 05:20:11.321";
Date fullDate = DateUtils.parseDate(dateWithFullTime);

String dateWithMins = "2025-08-04 05:20";
Date minsDate = DateUtils.parseDate(dateWithMins);

String date = "2025-08-04";
Date dayDate = DateUtils.parseDate(date);

If you want to parse an instant ISO format string, you can do it with the following method:

String instantISODate = "2025-08-04T04:20:11.321Z";
Date instantDate = DateUtils.parseISOInstant(instantISODate);

You can also choose the date format:

String dateWithCustomFormat = "2025/08/04";
Date customDate = DateUtils.parseDate(dateWithCustomFormat, "yyyy/MM/dd");

Date manipulation

You can add or substract time periods using the DateUtils.add* methods.

// You can also manipulate Date objects
Date newDateYear = DateUtils.addYears(actualDate, 20);
Date newDateMonth = DateUtils.addMonths(actualDate, 5);
Date newDateDay = DateUtils.addDays(actualDate, 2);
Date newDateHour = DateUtils.addHours(actualDate, 2);

// You can also substract by passing negative numbers
Date newDateMin = DateUtils.addMinutes(actualDate, -5);
Date newDateSec = DateUtils.addSeconds(actualDate, -10);
Date newDateMilli = DateUtils.addMillis(actualDate, -500);

If you need more flexibility you can convert the date to a GregorianCalendar, and do operations with that:

Date actualDate = new Date();
DateUtils.convertToGregorianCalendar(actualDate);

Get parts of a date

// You can obtain parts of Date as well
Date actualDate = new Date();
int year = DateUtils.getYear(actualDate);
int month = DateUtils.getMonth(actualDate);
int day = DateUtils.getDay(actualDate);
int hours = DateUtils.getHours(actualDate);
int minutes = DateUtils.getMinutes(actualDate);
int seconds = DateUtils.getSeconds(actualDate);
int millis = DateUtils.getMillis(actualDate);

Date creation

You can create a Date object from parts of it:

// Create date of 2025-08-04
Date date = DateUtils.createDate(2025, 8, 4);

// Create date of 2025-08-04 05:20:11.321
Date fulldate = DateUtils.create(2025, 8, 4, 5, 20, 11, 321);

Format date

There are a few methods to output a date as a string in various formats:

Date date = DateUtils.parseDate("2025-08-04 05:20:11.321");

// This would output 2025-08-04
String formattedDate = DateUtils.formatToISODate(date);

// This would output 2025-08-04 05:20:11.321
String formattedDateTime = DateUtils.formatToDateTime(date);

// This would output 2025-08-04T04:20:11.321Z
String formattedInstant = DateUtils.formatToISOInstant(date);

You can also choose your own format:

Date date = DateUtils.parseDate("2025-08-04 05:20:11.321");

// This would output 2025/08/04
String formattedDate = DateUtils.formatDate(date, "yyyy/MM/dd");

LocalDateTime

Parse date

String date = "2025-08-04 05:20:11.321";
LocalDateTime localDate = DateUtils.parseLocalDateTime(date);

// You can also parse other date formats
date = "2025-08-04T05:20:11";
localDate = DateUtils.parseLocalDateTime(date);

date = "2025-08-04 05:20:11";
localDate = DateUtils.parseLocalDateTime(date);

date = "2025-08-04 05:20";
localDate = DateUtils.parseLocalDateTime(date);

date = "2025-08-04";
localDate = DateUtils.parseLocalDateTime(date);

You can also parse from an instant date:

String date = "2025-08-04T04:20:11Z";
LocalDateTime localDate = DateUtils.parseLocalDateTimeFromISOInstant(date);

Format dates

LocalDateTime localDate = LocalDateTime.of(2025, 8, 4, 5, 20, 11, 321_000_000);

// This would output 2025-08-04
String formattedDate = DateUtils.formatToISODate(localDate);

// This would output 2025-08-04 05:20:11.321
String formattedDateTime = DateUtils.formatToDateTime(localDate);

// This would output the UTC date with the server current zone offset as 2025-08-04T04:20:11.321Z
String formattedInstant = DateUtils.formatToISOInstant(localDate);

// This would output the date in UTC with the given offset as 2025-08-04T02:20:11.321Z
String formattedInstant = DateUtils.formatToISOInstant(localDate, ZoneOffset.of("+03:00"));

LocalDate

Parse date

String date = "2025-08-04";
LocalDate localDate = DateUtils.parseLocalDate(date);

Format date

LocalDate localDate = LocalDate.of(2025, 8, 4);

// This would output 2025-08-04
String formattedDate = DateUtils.formatToISODate(localDate);

LocalTime

Parse time

String date = "05:20:11";
LocalTime localTime = DateUtils.parseLocalTime(date);

date = "05:20";
localTime = DateUtils.parseLocalTime(date);

Format time

LocalTime localTime = LocalTime.of(5, 20, 11);

// This would output 05:20:11
String formattedDate = DateUtils.formatToISODate(localDate);

// This would output 05:20
formattedDate = DateUtils.formatToHourAndMinute(localDate);

OffsetDateTime

Parse date

String date = "2025-08-04 05:20:11.321";
OffsetDateTime offsetDate = DateUtils.parseOffsetDateTime(date);

// You can also parse other date formats
date = "2025-08-04T04:20:11+0100";
offsetDate = DateUtils.parseOffsetDateTime(date);

date = "2025-08-04T04:20:11Z";
offsetDate = DateUtils.parseOffsetDateTime(date);

date = "2025-08-04T05:20:11";
offsetDate = DateUtils.parseOffsetDateTime(date);

date = "2025-08-04 05:20:11";
offsetDate = DateUtils.parseOffsetDateTime(date);

date = "2025-08-04 05:20";
offsetDate = DateUtils.parseOffsetDateTime(date);

date = "2025-08-04";
offsetDate = DateUtils.parseOffsetDateTime(date);

Format dates

OffsetDateTime offsetDate = OffsetDateTime.of(LocalDateTime.of(2025, 8, 4, 5, 20, 11, 321_000_000), ZoneOffset.of("+01:00"));

// This would output 2025-08-04
String formattedDate = DateUtils.formatToISODate(offsetDate);

// This would output 2025-08-04 05:20:11.321
String formattedDateTime = DateUtils.formatToDateTime(offsetDate);

// This would output the UTC date with the OffsetDateTime object offset as 2025-08-04T04:20:11.321Z
String formattedInstant = DateUtils.formatToISOInstant(offsetDate);

OffsetTime

Parse time

String date = "04:20:11+01:00";
OffsetTime offsetTime = DateUtils.parseOffsetTime(date);

// You can also parse other date formats
date = "04:20:11Z";
offsetTime = DateUtils.parseOffsetTime(date);

date = "05:20:11";
offsetTime = DateUtils.parseOffsetTime(date);

date = "05:20";
offsetTime = DateUtils.parseOffsetTime(date);

Format time

OffsetTime offsetTime = OffsetTime.of(LocalTime.of(5, 20, 11), ZoneOffset.of("+01:00"));

// This would output 05:20:11+01:00
String formattedDate = DateUtils.formatToISODate(offsetTime);