Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.dateinstant;

import java.time.Instant;
import java.util.Date;

public final class DateInstantConverter {

private DateInstantConverter() {
}

public static Instant toInstant(Date date) {
if (date == null) {
return null;
}
return date.toInstant();
}

public static Date toDate(Instant instant) {
if (instant == null) {
return null;
}
return Date.from(instant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.baeldung.dateinstant;

import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.*;

public class DateInstantConverterUnitTest {

@Test
void shouldConvertDateToInstant() {
Date date = new Date(1708752000000L);

Instant instant = DateInstantConverter.toInstant(date);

assertNotNull(instant);
assertEquals(date.getTime(), instant.toEpochMilli());
}

@Test
void shouldConvertInstantToDate() {
Instant instant = Instant.ofEpochMilli(1708752000000L);

Date date = DateInstantConverter.toDate(instant);

assertNotNull(date);
assertEquals(instant.toEpochMilli(), date.getTime());
}

@Test
void shouldReturnNullWhenDateIsNull() {
Instant instant = DateInstantConverter.toInstant(null);
assertNull(instant);
}

@Test
void shouldReturnNullWhenInstantIsNull() {
Date date = DateInstantConverter.toDate(null);
assertNull(date);
}

@Test
void shouldPreserveMillisecondPrecisionInRoundTrip() {
Instant originalInstant = Instant.now();

Date date = DateInstantConverter.toDate(originalInstant);
Instant convertedBack = DateInstantConverter.toInstant(date);

assertEquals(originalInstant.toEpochMilli(),
convertedBack.toEpochMilli());
}

@Test
void shouldTruncateNanosecondsWhenConvertingToDate() {
Instant instantWithNanos = Instant.ofEpochSecond(1000, 123456789);

Date date = DateInstantConverter.toDate(instantWithNanos);
Instant convertedBack = DateInstantConverter.toInstant(date);

assertEquals(instantWithNanos.toEpochMilli(),
convertedBack.toEpochMilli());
}
}