对 Java 中使用的日期和时间文章进行解读

This commit is contained in:
2025-01-16 08:31:01 -05:00
parent 51c254784c
commit a17a5ef5b5
17 changed files with 229 additions and 5 deletions
@@ -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"));
}
}