Initial version of the code.
This commit is contained in:
parent
504348e3ad
commit
4474ffdc85
@ -7,4 +7,5 @@ This module contains articles about date operations in Java.
|
|||||||
- [Checking If Two Java Dates Are on the Same Day](https://www.baeldung.com/java-check-two-dates-on-same-day)
|
- [Checking If Two Java Dates Are on the Same Day](https://www.baeldung.com/java-check-two-dates-on-same-day)
|
||||||
- [Converting Java Date to OffsetDateTime](https://www.baeldung.com/java-convert-date-to-offsetdatetime)
|
- [Converting Java Date to OffsetDateTime](https://www.baeldung.com/java-convert-date-to-offsetdatetime)
|
||||||
- [How to Set the JVM Time Zone](https://www.baeldung.com/java-jvm-time-zone)
|
- [How to Set the JVM Time Zone](https://www.baeldung.com/java-jvm-time-zone)
|
||||||
|
- [Extracting the Day of the Week](http://inprogress.baeldung.com/wp-admin/post.php?post=184664&action=edit&classic-editor)
|
||||||
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.datetime.dayofweek;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.DayOfWeek;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.TextStyle;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class DayOfWeekExtractor {
|
||||||
|
|
||||||
|
public static int getDayNumberOld(Date date) {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(date);
|
||||||
|
return cal.get(Calendar.DAY_OF_WEEK);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDayStringOld(Date date, Locale locale ) {
|
||||||
|
DateFormat formatter = new SimpleDateFormat("EEEE", locale);
|
||||||
|
return formatter.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getDayNumberNew(LocalDate date) {
|
||||||
|
DayOfWeek day = date.getDayOfWeek();
|
||||||
|
return day.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDayStringNew(LocalDate date, Locale locale ) {
|
||||||
|
DayOfWeek day = date.getDayOfWeek();
|
||||||
|
return day.getDisplayName(TextStyle.FULL, locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.datetime.dayofweek;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DayOfWeekExtractorUnitTest {
|
||||||
|
|
||||||
|
private DateFormat oldDateParser = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFeb29_2020_thenOldSaturdayNumber() throws ParseException {
|
||||||
|
assertThat(DayOfWeekExtractor.getDayNumberOld(oldDateParser.parse("2020-02-29")) == Calendar.SATURDAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFeb29_2020_and_localeUS_thenOldSaturdayText() throws ParseException {
|
||||||
|
assertThat("Saturday".equals(DayOfWeekExtractor.getDayStringOld(oldDateParser.parse("2020-02-29"), Locale.US)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFeb29_2020_and_localeDE_thenOldSaturdayText() throws ParseException {
|
||||||
|
assertThat("Samstag".equals(DayOfWeekExtractor.getDayStringOld(oldDateParser.parse("2020-02-29"), Locale.GERMANY)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFeb29_2020_thenNewSaturdayNumber() throws ParseException {
|
||||||
|
assertThat(DayOfWeekExtractor.getDayNumberNew(LocalDate.parse("2020-02-29")) == Calendar.SATURDAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFeb29_2020_and_localeUS_thenNewSaturdayText() throws ParseException {
|
||||||
|
assertThat("Saturday".equals(DayOfWeekExtractor.getDayStringOld(oldDateParser.parse("2020-02-29"), Locale.US)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFeb29_2020_and_localeDE_thenNewSaturdayText() throws ParseException {
|
||||||
|
assertThat("Samstag".equals(DayOfWeekExtractor.getDayStringOld(oldDateParser.parse("2020-02-29"), Locale.GERMANY)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user