Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Created January 11, 2018 21:03
Show Gist options
  • Save fmbenhassine/6fade9336c886f9e6f968d5c521ee02a to your computer and use it in GitHub Desktop.
Save fmbenhassine/6fade9336c886f9e6f968d5c521ee02a to your computer and use it in GitHub Desktop.
Going back and forth between Date and String is not guaranteed to work..
package org.springframework.batch.item;
import org.junit.Test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.junit.Assert.assertEquals;
public class DateFormatTests {
@Test
public void testSimpleDateFormatWithDefaultFormat() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat();
Date date = new Date();
String dateAsString = dateFormat.format(date);
Date sameDate = dateFormat.parse(dateAsString);
assertEquals(date, sameDate);
}
@Test
public void testSimpleDateFormatWithSpecificFormat() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // choose whatever valid format you like
Date date = new Date();
String dateAsString = dateFormat.format(date);
Date sameDate = dateFormat.parse(dateAsString);
assertEquals(date, sameDate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment