Skip to content

Commit

Permalink
add helper methods to get DayOfWeek
Browse files Browse the repository at this point in the history
  • Loading branch information
ymo-sci committed May 6, 2024
1 parent 076fb58 commit ed06937
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/main/java/sirius/kernel/nls/NLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -548,7 +549,7 @@ public String translated() {
}

/**
* Converts a given integer ({@code Calendar.Monday...Calendar.Sunday}) into textual their representation.
* Converts a given integer ({@code Calendar.Monday...Calendar.Sunday}) into their textual representation.
*
* @param day the weekday to be translated. Use constants {@link Calendar#MONDAY} etc.
* @return the name of the given weekday in the current language
Expand All @@ -567,6 +568,24 @@ public static String getDayOfWeek(int day) {
};
}

/**
* Converts a given weekday ({@link DayOfWeek#MONDAY} ... {@link DayOfWeek#SUNDAY}) into the textual representation.
*
* @param day the weekday to be translated. Use constants {@link DayOfWeek#MONDAY} etc.
* @return the name of the given weekday in the current language
*/
public static String getDayOfWeek(DayOfWeek day) {
return switch (day) {
case DayOfWeek.MONDAY -> CommonKeys.MONDAY.translated();
case DayOfWeek.TUESDAY -> CommonKeys.TUESDAY.translated();
case DayOfWeek.WEDNESDAY -> CommonKeys.WEDNESDAY.translated();
case DayOfWeek.THURSDAY -> CommonKeys.THURSDAY.translated();
case DayOfWeek.FRIDAY -> CommonKeys.FRIDAY.translated();
case DayOfWeek.SATURDAY -> CommonKeys.SATURDAY.translated();
case DayOfWeek.SUNDAY -> CommonKeys.SUNDAY.translated();
};
}

/**
* Returns a two letter abbreviation of the name of the given day, like {@code "Mo"}.
*
Expand All @@ -578,6 +597,16 @@ public static String getDayOfWeekShort(int day) {
return Value.of(getDayOfWeek(day)).substring(0, 2);
}

/**
* Returns a two letter abbreviation of the name of the given day, like {@code "Mo"}.
*
* @param day the weekday to be translated. Use constants {@link DayOfWeek#MONDAY} etc.
* @return returns the first two letters of the name
*/
public static String getDayOfWeekShort(DayOfWeek day) {
return Value.of(getDayOfWeek(day)).substring(0, 2);
}

/**
* Returns the name of the given month in the current language
*
Expand Down

0 comments on commit ed06937

Please sign in to comment.