[BAEL-11403] - Moved articles out of core-java (part 4)

This commit is contained in:
amit2103
2018-12-30 16:30:36 +05:30
parent 9ffbe2bfcb
commit eb4928b972
28 changed files with 19 additions and 99 deletions

View File

@@ -0,0 +1,13 @@
package com.baeldung.zoneddatetime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class OffsetDateTimeExample {
public OffsetDateTime getCurrentTimeByZoneOffset(String offset) {
ZoneOffset zoneOffSet= ZoneOffset.of(offset);
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
return date;
}
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.zoneddatetime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class OffsetTimeExample {
public OffsetTime getCurrentTimeByZoneOffset(String offset) {
ZoneOffset zoneOffSet = ZoneOffset.of(offset);
OffsetTime time = OffsetTime.now(zoneOffSet);
return time;
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.zoneddatetime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneDateTimeExample {
public ZonedDateTime getCurrentTimeByZoneId(String region) {
ZoneId zone = ZoneId.of(region);
ZonedDateTime date = ZonedDateTime.now(zone);
return date;
}
public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) {
ZoneId destZoneId = ZoneId.of(destZone);
ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId);
return destDate;
}
}

View File

@@ -8,6 +8,8 @@ import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;

View File

@@ -0,0 +1,60 @@
package com.baeldung.simpledateformat;
import org.junit.Test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SimpleDateFormatUnitTest {
private static final Logger logger = Logger.getLogger(SimpleDateFormatUnitTest.class.getName());
@Test
public void givenSpecificDate_whenFormatted_thenCheckFormatCorrect() throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
assertEquals("24-05-1977", formatter.format(new Date(233345223232L)));
}
@Test
public void givenSpecificDate_whenFormattedUsingDateFormat_thenCheckFormatCorrect() throws Exception {
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
assertEquals("5/24/77", formatter.format(new Date(233345223232L)));
}
@Test
public void givenStringDate_whenParsed_thenCheckDateCorrect() throws Exception{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date myDate = new Date(233276400000L);
Date parsedDate = formatter.parse("24-05-1977");
assertEquals(myDate.getTime(), parsedDate.getTime());
}
@Test
public void givenFranceLocale_whenFormatted_thenCheckFormatCorrect() throws Exception{
SimpleDateFormat franceDateFormatter = new SimpleDateFormat("EEEEE dd-MMMMMMM-yyyy", Locale.FRANCE);
Date myWednesday = new Date(1539341312904L);
assertTrue(franceDateFormatter.format(myWednesday).startsWith("vendredi"));
}
@Test
public void given2TimeZones_whenFormatted_thenCheckTimeDifference() throws Exception {
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE dd-MMM-yy HH:mm:ssZ");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));
logger.info(simpleDateFormat.format(now));
//change the date format
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
logger.info(simpleDateFormat.format(now));
}
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class OffsetDateTimeExampleUnitTest {
OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String offset = "+02:00";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset);
assertTrue(time.getOffset()
.equals(ZoneOffset.of(offset)));
}
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class OffsetTimeExampleUnitTest {
OffsetTimeExample offsetTimeExample = new OffsetTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String offset = "+02:00";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset);
assertTrue(time.getOffset()
.equals(ZoneOffset.of(offset)));
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZoneDateTimeExampleUnitTest {
ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample();
@Test
public void givenZone_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone);
assertTrue(time.getZone()
.equals(ZoneId.of(zone)));
}
@Test
public void givenZones_whenConvertDateByZone_thenGetConstantDiff() {
String sourceZone = "Europe/Berlin";
String destZone = "Asia/Tokyo";
ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone);
ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone);
assertTrue(sourceDate.toInstant()
.compareTo(destDate.toInstant()) == 0);
}
}