BAEL-3911 (#8939)
* Initial version of the code. * Addressed PR comment.
This commit is contained in:
		
							parent
							
								
									8cfb3a1db8
								
							
						
					
					
						commit
						1062b4fef6
					
				| @ -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