Created
January 11, 2018 21:03
-
-
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..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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