对时区中的文章 Java 中的 ZoneOffset 进行更新 #36
1
.idea/compiler.xml
generated
1
.idea/compiler.xml
generated
@ -33,6 +33,7 @@
|
||||
<module name="core-java-streams" />
|
||||
<module name="core-java-io-2" />
|
||||
<module name="core-java-io" />
|
||||
<module name="core-java-8-datetime" />
|
||||
<module name="discourse" />
|
||||
<module name="image-processing" />
|
||||
<module name="jackson-jr" />
|
||||
|
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@ -15,6 +15,8 @@
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-11/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8-2/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8-2/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8-datetime/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8-datetime/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-9/src/main/java" charset="UTF-8" />
|
||||
|
@ -1,13 +1,13 @@
|
||||
## Java 8+ Date and Time API
|
||||
## Java 8+ Date 和 Time API
|
||||
|
||||
This module contains articles about the Date and Time API introduced with Java 8.
|
||||
本模块中包含的内容有关 Java 8 中使用日期和时间的 API。
|
||||
|
||||
### 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 and Time in Java](https://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)
|
||||
- [Java 中的 ZoneOffset](https://www.isharkfly.com/t/java-zoneoffset/16803)
|
||||
- [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)
|
||||
|
@ -8,9 +8,9 @@
|
||||
<name>core-java-8-datetime</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<groupId>com.ossez.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class CalendarUtils {
|
||||
public static Calendar getPlusDays(Date date, int amount) throws ParseException {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.DAY_OF_YEAR, amount);
|
||||
return calendar;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
public class DateUtils {
|
||||
|
||||
public static Date getNow() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
public static Date getDate(long millis) {
|
||||
return new Date(millis);
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString, String pattern) throws ParseException {
|
||||
return new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class UseSimpleDateFormat {
|
||||
|
||||
public String useFormat() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
|
||||
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
|
||||
Date date = new Date(1725437542000L);
|
||||
return sdf.format(date);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
public static Date getNow() {
|
||||
return new Date(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString) {
|
||||
return Date.valueOf(dateAsString);
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Date(customUtilDate.getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class TimeUtils {
|
||||
|
||||
public static Time getNow() {
|
||||
return new Time(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Time getTime(String timeAsString) {
|
||||
return Time.valueOf(timeAsString);
|
||||
}
|
||||
|
||||
public static Time getTime(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Time(customUtilDate.getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class TimestampUtils {
|
||||
|
||||
public static Timestamp getNow() {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Timestamp getTimestamp(String timestampAsString) {
|
||||
return Timestamp.valueOf(timestampAsString);
|
||||
}
|
||||
|
||||
public static Timestamp getTimestamp(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Timestamp(customUtilDate.getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CalendarUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException {
|
||||
Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd");
|
||||
Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd");
|
||||
assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime());
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
|
||||
long milliseconds = new Date(2020 - 1900, 0, 1).getTime();
|
||||
assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseSimpleDateFormatTest {
|
||||
private UseSimpleDateFormat useSimpleDateFormat = new UseSimpleDateFormat();
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalTime() {
|
||||
Assert.assertEquals("2024-09-04 13:42:22 +0530", useSimpleDateFormat.useFormat());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
DateUtils.getDate("2020 01 01");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.sql.TimeUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
public class TimeUtilsUnitTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
TimeUtils.getTime("10 11 12");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss"));
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.datetime.sql;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TimestampUtilsUnitTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenTimestampAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
TimestampUtils.getTimestamp("2020/01/01 10:11-12");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimestampAndPattern_thenTimestampIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(TimestampUtils.getTimestamp("2020-01-01 10:11:12"), TimestampUtils.getTimestamp("2020/01/01 10:11-12", "yyyy/MM/dd hh:mm-ss"));
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@
|
||||
<modules>
|
||||
<module>core-java</module>
|
||||
<module>core-java-8</module>
|
||||
<module>core-java-8-datetime</module>
|
||||
<module>core-java-9</module>
|
||||
<!-- <module>core-java-10</module>-->
|
||||
<module>core-java-11</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user