How to determine day of week by passing specific date?

前端 未结 25 1859
夕颜
夕颜 2020-11-22 05:12

For Example I have the date: \"23/2/2010\" (23th Feb 2010). I want to pass it to a function which would return the day of week. How can I do this?

I

25条回答
  •  野性不改
    2020-11-22 05:42

    java.time

    Using java.time framework built into Java 8 and later.

    The DayOfWeek enum can generate a String of the day’s name automatically localized to the human language and cultural norms of a Locale. Specify a TextStyle to indicate you want long form or abbreviated name.

    import java.time.LocalDate
    import java.time.format.DateTimeFormatter
    import java.time.format.TextStyle
    import java.util.Locale
    import java.time.DayOfWeek;
    
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
    LocalDate date = LocalDate.parse("23/2/2010", formatter); // LocalDate = 2010-02-23
    DayOfWeek dow = date.getDayOfWeek();  // Extracts a `DayOfWeek` enum object.
    String output = dow.getDisplayName(TextStyle.SHORT, Locale.US); // String = Tue
    

提交回复
热议问题