#BAEL-16646 Split Java dates articles regarding Java 8. Also moved [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone) from core-java-8 to the new java-dates-java8 module (the code was already in java-dates) and moved some missing files for the article Extracting... to java-dates-computations.

java-dates now contains only two classes, which are relevant to the previous PR which will be shortly updated. One seems related to java-dates-string but it's unclear which article it belongs to and needs to be investigated.
This commit is contained in:
Alessio Stalla
2019-10-02 16:59:08 +02:00
parent 6ce78b1dde
commit 80638ba2ca
41 changed files with 359 additions and 460 deletions

View File

@@ -23,7 +23,6 @@
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic) - [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) - [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time)
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
- [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance) - [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance)
- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects) - [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects)
- [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic) - [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic)

View File

@@ -1,18 +1,18 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.time.LocalDate; import java.time.LocalDate;
public class LocalDateExtractYearMonthDayIntegerValues { public class LocalDateExtractYearMonthDayIntegerValues {
int getYear(LocalDate localDate) { int getYear(LocalDate localDate) {
return localDate.getYear(); return localDate.getYear();
} }
int getMonth(LocalDate localDate) { int getMonth(LocalDate localDate) {
return localDate.getMonthValue(); return localDate.getMonthValue();
} }
int getDay(LocalDate localDate) { int getDay(LocalDate localDate) {
return localDate.getDayOfMonth(); return localDate.getDayOfMonth();
} }
} }

View File

@@ -1,18 +1,18 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public class LocalDateTimeExtractYearMonthDayIntegerValues { public class LocalDateTimeExtractYearMonthDayIntegerValues {
int getYear(LocalDateTime localDateTime) { int getYear(LocalDateTime localDateTime) {
return localDateTime.getYear(); return localDateTime.getYear();
} }
int getMonth(LocalDateTime localDateTime) { int getMonth(LocalDateTime localDateTime) {
return localDateTime.getMonthValue(); return localDateTime.getMonthValue();
} }
int getDay(LocalDateTime localDateTime) { int getDay(LocalDateTime localDateTime) {
return localDateTime.getDayOfMonth(); return localDateTime.getDayOfMonth();
} }
} }

View File

@@ -1,18 +1,18 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
public class OffsetDateTimeExtractYearMonthDayIntegerValues { public class OffsetDateTimeExtractYearMonthDayIntegerValues {
int getYear(OffsetDateTime offsetDateTime) { int getYear(OffsetDateTime offsetDateTime) {
return offsetDateTime.getYear(); return offsetDateTime.getYear();
} }
int getMonth(OffsetDateTime offsetDateTime) { int getMonth(OffsetDateTime offsetDateTime) {
return offsetDateTime.getMonthValue(); return offsetDateTime.getMonthValue();
} }
int getDay(OffsetDateTime offsetDateTime) { int getDay(OffsetDateTime offsetDateTime) {
return offsetDateTime.getDayOfMonth(); return offsetDateTime.getDayOfMonth();
} }
} }

View File

@@ -1,18 +1,18 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
public class ZonedDateTimeExtractYearMonthDayIntegerValues { public class ZonedDateTimeExtractYearMonthDayIntegerValues {
int getYear(ZonedDateTime zonedDateTime) { int getYear(ZonedDateTime zonedDateTime) {
return zonedDateTime.getYear(); return zonedDateTime.getYear();
} }
int getMonth(ZonedDateTime zonedDateTime) { int getMonth(ZonedDateTime zonedDateTime) {
return zonedDateTime.getMonthValue(); return zonedDateTime.getMonthValue();
} }
int getDay(ZonedDateTime zonedDateTime) { int getDay(ZonedDateTime zonedDateTime) {
return zonedDateTime.getDayOfMonth(); return zonedDateTime.getDayOfMonth();
} }
} }

View File

@@ -1,45 +1,45 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class DateExtractYearMonthDayIntegerValuesUnitTest { public class DateExtractYearMonthDayIntegerValuesUnitTest {
DateExtractYearMonthDayIntegerValues extractYearMonthDateIntegerValues = new DateExtractYearMonthDayIntegerValues(); DateExtractYearMonthDayIntegerValues extractYearMonthDateIntegerValues = new DateExtractYearMonthDayIntegerValues();
Date date; Date date;
@Before @Before
public void setup() throws ParseException public void setup() throws ParseException
{ {
date=new SimpleDateFormat("dd-MM-yyyy").parse("01-03-2018"); date=new SimpleDateFormat("dd-MM-yyyy").parse("01-03-2018");
} }
@Test @Test
public void whenGetYear_thenCorrectYear() public void whenGetYear_thenCorrectYear()
{ {
int actualYear=extractYearMonthDateIntegerValues.getYear(date); int actualYear=extractYearMonthDateIntegerValues.getYear(date);
assertThat(actualYear,is(2018)); assertThat(actualYear,is(2018));
} }
@Test @Test
public void whenGetMonth_thenCorrectMonth() public void whenGetMonth_thenCorrectMonth()
{ {
int actualMonth=extractYearMonthDateIntegerValues.getMonth(date); int actualMonth=extractYearMonthDateIntegerValues.getMonth(date);
assertThat(actualMonth,is(02)); assertThat(actualMonth,is(02));
} }
@Test @Test
public void whenGetDay_thenCorrectDay() public void whenGetDay_thenCorrectDay()
{ {
int actualDayOfMonth=extractYearMonthDateIntegerValues.getDay(date); int actualDayOfMonth=extractYearMonthDateIntegerValues.getDay(date);
assertThat(actualDayOfMonth,is(01)); assertThat(actualDayOfMonth,is(01));
} }
} }

View File

@@ -1,36 +1,36 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.time.LocalDate; import java.time.LocalDate;
import org.junit.Test; import org.junit.Test;
public class LocalDateExtractYearMonthDayIntegerValuesUnitTest { public class LocalDateExtractYearMonthDayIntegerValuesUnitTest {
LocalDateExtractYearMonthDayIntegerValues localDateExtractYearMonthDayIntegerValues=new LocalDateExtractYearMonthDayIntegerValues(); LocalDateExtractYearMonthDayIntegerValues localDateExtractYearMonthDayIntegerValues=new LocalDateExtractYearMonthDayIntegerValues();
LocalDate localDate=LocalDate.parse("2007-12-03"); LocalDate localDate=LocalDate.parse("2007-12-03");
@Test @Test
public void whenGetYear_thenCorrectYear() public void whenGetYear_thenCorrectYear()
{ {
int actualYear=localDateExtractYearMonthDayIntegerValues.getYear(localDate); int actualYear=localDateExtractYearMonthDayIntegerValues.getYear(localDate);
assertThat(actualYear,is(2007)); assertThat(actualYear,is(2007));
} }
@Test @Test
public void whenGetMonth_thenCorrectMonth() public void whenGetMonth_thenCorrectMonth()
{ {
int actualMonth=localDateExtractYearMonthDayIntegerValues.getMonth(localDate); int actualMonth=localDateExtractYearMonthDayIntegerValues.getMonth(localDate);
assertThat(actualMonth,is(12)); assertThat(actualMonth,is(12));
} }
@Test @Test
public void whenGetDay_thenCorrectDay() public void whenGetDay_thenCorrectDay()
{ {
int actualDayOfMonth=localDateExtractYearMonthDayIntegerValues.getDay(localDate); int actualDayOfMonth=localDateExtractYearMonthDayIntegerValues.getDay(localDate);
assertThat(actualDayOfMonth,is(03)); assertThat(actualDayOfMonth,is(03));
} }
} }

View File

@@ -1,36 +1,36 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import org.junit.Test; import org.junit.Test;
public class LocalDateTimeExtractYearMonthDayIntegerValuesUnitTest { public class LocalDateTimeExtractYearMonthDayIntegerValuesUnitTest {
LocalDateTimeExtractYearMonthDayIntegerValues localDateTimeExtractYearMonthDayIntegerValues = new LocalDateTimeExtractYearMonthDayIntegerValues(); LocalDateTimeExtractYearMonthDayIntegerValues localDateTimeExtractYearMonthDayIntegerValues = new LocalDateTimeExtractYearMonthDayIntegerValues();
LocalDateTime localDateTime=LocalDateTime.parse("2007-12-03T10:15:30"); LocalDateTime localDateTime=LocalDateTime.parse("2007-12-03T10:15:30");
@Test @Test
public void whenGetYear_thenCorrectYear() public void whenGetYear_thenCorrectYear()
{ {
int actualYear=localDateTimeExtractYearMonthDayIntegerValues.getYear(localDateTime); int actualYear=localDateTimeExtractYearMonthDayIntegerValues.getYear(localDateTime);
assertThat(actualYear,is(2007)); assertThat(actualYear,is(2007));
} }
@Test @Test
public void whenGetMonth_thenCorrectMonth() public void whenGetMonth_thenCorrectMonth()
{ {
int actualMonth=localDateTimeExtractYearMonthDayIntegerValues.getMonth(localDateTime); int actualMonth=localDateTimeExtractYearMonthDayIntegerValues.getMonth(localDateTime);
assertThat(actualMonth,is(12)); assertThat(actualMonth,is(12));
} }
@Test @Test
public void whenGetDay_thenCorrectDay() public void whenGetDay_thenCorrectDay()
{ {
int actualDayOfMonth=localDateTimeExtractYearMonthDayIntegerValues.getDay(localDateTime); int actualDayOfMonth=localDateTimeExtractYearMonthDayIntegerValues.getDay(localDateTime);
assertThat(actualDayOfMonth,is(03)); assertThat(actualDayOfMonth,is(03));
} }
} }

View File

@@ -1,36 +1,36 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import org.junit.Test; import org.junit.Test;
public class OffsetDateTimeExtractYearMonthDayIntegerValuesUnitTest { public class OffsetDateTimeExtractYearMonthDayIntegerValuesUnitTest {
OffsetDateTimeExtractYearMonthDayIntegerValues offsetDateTimeExtractYearMonthDayIntegerValues = new OffsetDateTimeExtractYearMonthDayIntegerValues(); OffsetDateTimeExtractYearMonthDayIntegerValues offsetDateTimeExtractYearMonthDayIntegerValues = new OffsetDateTimeExtractYearMonthDayIntegerValues();
OffsetDateTime offsetDateTime=OffsetDateTime.parse("2007-12-03T10:15:30+01:00"); OffsetDateTime offsetDateTime=OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
@Test @Test
public void whenGetYear_thenCorrectYear() public void whenGetYear_thenCorrectYear()
{ {
int actualYear=offsetDateTimeExtractYearMonthDayIntegerValues.getYear(offsetDateTime); int actualYear=offsetDateTimeExtractYearMonthDayIntegerValues.getYear(offsetDateTime);
assertThat(actualYear,is(2007)); assertThat(actualYear,is(2007));
} }
@Test @Test
public void whenGetMonth_thenCorrectMonth() public void whenGetMonth_thenCorrectMonth()
{ {
int actualMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getMonth(offsetDateTime); int actualMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getMonth(offsetDateTime);
assertThat(actualMonth,is(12)); assertThat(actualMonth,is(12));
} }
@Test @Test
public void whenGetDay_thenCorrectDay() public void whenGetDay_thenCorrectDay()
{ {
int actualDayOfMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getDay(offsetDateTime); int actualDayOfMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getDay(offsetDateTime);
assertThat(actualDayOfMonth,is(03)); assertThat(actualDayOfMonth,is(03));
} }
} }

View File

@@ -1,36 +1,36 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import org.junit.Test; import org.junit.Test;
public class ZonedDateTimeExtractYearMonthDayIntegerValuesUnitTest { public class ZonedDateTimeExtractYearMonthDayIntegerValuesUnitTest {
ZonedDateTimeExtractYearMonthDayIntegerValues zonedDateTimeExtractYearMonthDayIntegerValues = new ZonedDateTimeExtractYearMonthDayIntegerValues(); ZonedDateTimeExtractYearMonthDayIntegerValues zonedDateTimeExtractYearMonthDayIntegerValues = new ZonedDateTimeExtractYearMonthDayIntegerValues();
ZonedDateTime zonedDateTime=ZonedDateTime.parse("2007-12-03T10:15:30+01:00"); ZonedDateTime zonedDateTime=ZonedDateTime.parse("2007-12-03T10:15:30+01:00");
@Test @Test
public void whenGetYear_thenCorrectYear() public void whenGetYear_thenCorrectYear()
{ {
int actualYear=zonedDateTimeExtractYearMonthDayIntegerValues.getYear(zonedDateTime); int actualYear=zonedDateTimeExtractYearMonthDayIntegerValues.getYear(zonedDateTime);
assertThat(actualYear,is(2007)); assertThat(actualYear,is(2007));
} }
@Test @Test
public void whenGetMonth_thenCorrectMonth() public void whenGetMonth_thenCorrectMonth()
{ {
int actualMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getMonth(zonedDateTime); int actualMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getMonth(zonedDateTime);
assertThat(actualMonth,is(12)); assertThat(actualMonth,is(12));
} }
@Test @Test
public void whenGetDay_thenCorrectDay() public void whenGetDay_thenCorrectDay()
{ {
int actualDayOfMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getDay(zonedDateTime); int actualDayOfMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getDay(zonedDateTime);
assertThat(actualDayOfMonth,is(03)); assertThat(actualDayOfMonth,is(03));
} }
} }

View File

@@ -3,4 +3,13 @@
This module contains articles about the Date and Time API introduced with Java 8. This module contains articles about the Date and Time API introduced with Java 8.
### Relevant Articles: ### Relevant Articles:
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api) - [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)
- [Get the Current Date, Time and Timestamp in Java 8](http://www.baeldung.com/current-date-time-and-timestamp-in-java-8)
- [TemporalAdjuster in Java](http://www.baeldung.com/java-temporal-adjuster)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
- [Comparing Dates in Java](https://www.baeldung.com/java-comparing-dates)

View File

@@ -14,12 +14,28 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<version>${assertj.version}</version> <version>${assertj.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -47,6 +63,7 @@
<properties> <properties>
<maven.compiler.source>1.9</maven.compiler.source> <maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target> <maven.compiler.target>1.9</maven.compiler.target>
<joda-time.version>2.10</joda-time.version>
<!-- testing --> <!-- testing -->
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
</properties> </properties>

View File

@@ -1,72 +1,72 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class UseTimeZoneUnitTest { public class UseTimeZoneUnitTest {
/* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */ /* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
String timeZone = "Asia/Singapore"; String timeZone = "Asia/Singapore";
private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a"; private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a";
@Test @Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() { public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() {
Date nowUtc = new Date(); Date nowUtc = new Date();
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone); TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);
Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore); Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);
nowAsiaSingapore.setTime(nowUtc); nowAsiaSingapore.setTime(nowUtc);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone() System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone()
.getID(), simpleDateFormat.format(nowAsiaSingapore.getTime()))); .getID(), simpleDateFormat.format(nowAsiaSingapore.getTime())));
Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond()); Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone()); Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone());
} }
@Test @Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() { public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() {
Instant nowUtc = Instant.now(); Instant nowUtc = Instant.now();
ZoneId asiaSingapore = ZoneId.of(timeZone); ZoneId asiaSingapore = ZoneId.of(timeZone);
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore); ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(), System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN)))); nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN))));
Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond()); Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone()); Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
} }
@Test @Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() { public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() {
org.joda.time.Instant nowUtc = org.joda.time.Instant.now(); org.joda.time.Instant nowUtc = org.joda.time.Instant.now();
DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone); DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone);
DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore); DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);
System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(), System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
nowAsiaSingapore.toString(PATTERN))); nowAsiaSingapore.toString(PATTERN)));
Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis()); Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone()); Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
} }
} }

26
java-dates/.gitignore vendored
View File

@@ -1,26 +0,0 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@@ -1,13 +0,0 @@
=========
## Java Dates Cookbooks and Examples
### Relevant Articles:
- [TemporalAdjuster in Java](http://www.baeldung.com/java-temporal-adjuster)
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
- [Get the Current Date, Time and Timestamp in Java 8](http://www.baeldung.com/current-date-time-and-timestamp-in-java-8)
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)
- [Comparing Dates in Java](https://www.baeldung.com/java-comparing-dates)

View File

@@ -1,60 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>java-dates</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>java-dates</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
</dependencies>
<build>
<finalName>java-dates</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<joda-time.version>2.10</joda-time.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>

View File

@@ -1 +0,0 @@
### Relevant Articles:

View File

@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -1,13 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear