Merge pull request #8047 from alessiostalla/BAEL-16646-2

Bael 16646 2
This commit is contained in:
Josh Cummings
2019-10-26 14:25:57 -06:00
committed by GitHub
71 changed files with 601 additions and 463 deletions

View File

@@ -0,0 +1,15 @@
## Java Date/time computations Cookbooks and Examples
This module contains articles about date and time computations in Java.
### Relevant Articles:
- [Difference Between Two Dates in Java](http://www.baeldung.com/java-date-difference)
- [Get Date Without Time in Java](http://www.baeldung.com/java-date-without-time)
- [How to Get All Dates Between Two Dates?](http://www.baeldung.com/java-between-dates)
- [Extracting Year, Month and Day from Date in Java](http://www.baeldung.com/java-year-month-day)
- [Guide to java.util.GregorianCalendar](http://www.baeldung.com/java-gregorian-calendar)
- [Handling Daylight Savings Time in Java](http://www.baeldung.com/java-daylight-savings)
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)
- [Increment Date in Java](http://www.baeldung.com/java-increment-date)
- [Add Hours To a Date In Java](http://www.baeldung.com/java-add-hours-date)
- [Introduction to Joda-Time](http://www.baeldung.com/joda-time)

View File

@@ -0,0 +1,71 @@
<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>
<artifactId>core-java-datetime-computations</artifactId>
<version>${project.parent.version}</version>
<name>core-java-datetime-computations</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>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.darwinsys</groupId>
<artifactId>hirondelle-date4j</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-datetime-computations</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,28 +1,28 @@
package com.baeldung.datetime;
import java.util.Calendar;
import java.util.Date;
public class DateExtractYearMonthDayIntegerValues {
int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH);
}
int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}
package com.baeldung.datetime;
import java.util.Calendar;
import java.util.Date;
public class DateExtractYearMonthDayIntegerValues {
int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH);
}
int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,15 @@
## Java 8+ Date and Time API
This module contains articles about the Date and Time API introduced with Java 8.
### 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)
- [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

@@ -1,17 +1,16 @@
<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>
<artifactId>core-java-datetime-java8</artifactId>
<version>${project.parent.version}</version>
<name>core-java-datetime-java8</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
@@ -21,11 +20,10 @@
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
@@ -33,20 +31,15 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>com.darwinsys</groupId>
<artifactId>hirondelle-date4j</artifactId>
<version>RELEASE</version>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>java-dates</finalName>
<finalName>core-java-datetime-java8</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
@@ -68,10 +61,10 @@
</build>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<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,72 +1,72 @@
package com.baeldung.datetime;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Assert;
import org.junit.Test;
public class UseTimeZoneUnitTest {
/* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
String timeZone = "Asia/Singapore";
private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a";
@Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() {
Date nowUtc = new Date();
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);
Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);
nowAsiaSingapore.setTime(nowUtc);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone()
.getID(), simpleDateFormat.format(nowAsiaSingapore.getTime())));
Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone());
}
@Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() {
Instant nowUtc = Instant.now();
ZoneId asiaSingapore = ZoneId.of(timeZone);
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN))));
Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
}
@Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() {
org.joda.time.Instant nowUtc = org.joda.time.Instant.now();
DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone);
DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);
System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
nowAsiaSingapore.toString(PATTERN)));
Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
}
}
package com.baeldung.datetime;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Assert;
import org.junit.Test;
public class UseTimeZoneUnitTest {
/* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
String timeZone = "Asia/Singapore";
private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a";
@Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() {
Date nowUtc = new Date();
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);
Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);
nowAsiaSingapore.setTime(nowUtc);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone()
.getID(), simpleDateFormat.format(nowAsiaSingapore.getTime())));
Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone());
}
@Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() {
Instant nowUtc = Instant.now();
ZoneId asiaSingapore = ZoneId.of(timeZone);
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN))));
Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
}
@Test
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() {
org.joda.time.Instant nowUtc = org.joda.time.Instant.now();
DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone);
DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);
System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
nowAsiaSingapore.toString(PATTERN)));
Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis());
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
}
}

View File

@@ -0,0 +1,8 @@
## Java Time Measurements
This module contains articles about the measurement of time in Java.
### Relevant Articles:
- [Guide to the Java Clock Class](http://www.baeldung.com/java-clock)
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
- [Overriding System Time for Testing in Java](https://www.baeldung.com/java-override-system-time)

View File

@@ -0,0 +1,102 @@
<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.exception.numberformat</groupId>
<artifactId>core-java-time-measurements</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-time-measurements</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${asspectj.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-time-measurements</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
</argLine>
<disableXmlReport>true</disableXmlReport>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- util -->
<commons-math3.version>3.6.1</commons-math3.version>
<joda.version>2.10</joda.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<asspectj.version>1.8.9</asspectj.version>
<powermock.version>2.0.0-RC.4</powermock.version>
<jmockit.version>1.44</jmockit.version>
<!-- plugins -->
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@@ -21,7 +21,6 @@
- [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
- [What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid)
- [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle)
- [Guide to the Java Clock Class](http://www.baeldung.com/java-clock)
- [Importance of Main Manifest Attribute in a Self-Executing JAR](http://www.baeldung.com/java-jar-executable-manifest-main-class)
- [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler)
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)

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,24 +0,0 @@
=========
## Java Dates Cookbooks and Examples
### Relevant Articles:
- [TemporalAdjuster in Java](http://www.baeldung.com/java-temporal-adjuster)
- [Handling Daylight Savings Time in Java](http://www.baeldung.com/java-daylight-savings)
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
- [Difference Between Two Dates in Java](http://www.baeldung.com/java-date-difference)
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)
- [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)
- [Get Date Without Time in Java](http://www.baeldung.com/java-date-without-time)
- [How to Get All Dates Between Two Dates?](http://www.baeldung.com/java-between-dates)
- [Extracting Year, Month and Day from Date in Java](http://www.baeldung.com/java-year-month-day)
- [Guide to java.util.GregorianCalendar](http://www.baeldung.com/java-gregorian-calendar)
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)
- [Increment Date in Java](http://www.baeldung.com/java-increment-date)
- [Add Hours To a Date In Java](http://www.baeldung.com/java-add-hours-date)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)
- [Introduction to Joda-Time](https://www.baeldung.com/joda-time)

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

20
pom.xml
View File

@@ -393,8 +393,14 @@
<module>core-java-modules/core-java-streams</module>
<module>core-java-modules/core-java-function</module>
<module>core-java-modules/core-java-lang-math</module>
<!--<module>core-java-modules/core-java-datetime-conversion</module>--> <!-- We haven't upgraded to java 9.-->
<!--<module>core-java-modules/core-java-datetime-string</module>--> <!-- We haven't upgraded to java 9.-->
<!-- We haven't upgraded to java 9.-->
<!--
<module>core-java-modules/core-java-datetime-computations</module>
<module>core-java-modules/core-java-datetime-conversion</module>
<module>core-java-modules/core-java-datetime-java8</module>
<module>core-java-modules/core-java-datetime-string</module>
<module>core-java-modules/core-java-time-measurements</module>
-->
<module>core-java-modules/core-java-text</module>
<module>core-java-modules/core-java-lambdas</module>
<!--<module>core-java-modules/core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
@@ -1157,8 +1163,14 @@
<module>core-java-modules/core-java-streams</module>
<module>core-java-modules/core-java-function</module>
<module>core-java-modules/core-java-lang-math</module>
<!--<module>core-java-modules/core-java-datetime-conversion</module>--> <!-- We haven't upgraded to java 9.-->
<!--<module>core-java-modules/core-java-datetime-string</module>--> <!-- We haven't upgraded to java 9.-->
<!-- We haven't upgraded to java 9.-->
<!--
<module>core-java-modules/core-java-datetime-computations</module>
<module>core-java-modules/core-java-datetime-conversion</module>
<module>core-java-modules/core-java-datetime-java8</module>
<module>core-java-modules/core-java-datetime-string</module>
<module>core-java-modules/core-java-time-measurements</module>
-->
<module>core-java-modules/core-java-text</module>
<!--<module>core-java-modules/core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!--<module>core-java-modules/core-java-os</module> --> <!-- We haven't upgraded to java 9.-->