How to determine day of week by passing specific date?

前端 未结 25 1431
夕颜
夕颜 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:35
    Calendar cal = Calendar.getInstance(desired date);
    cal.setTimeInMillis(System.currentTimeMillis());
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    

    Get the day value by providing the current time stamp.

    0 讨论(0)
  • 2020-11-22 05:36

    You can use below method to get Day of the Week by passing a specific date,

    Here for the set method of Calendar class, Tricky part is the index for the month parameter will starts from 0.

    public static String getDay(int day, int month, int year) {
    
            Calendar cal = Calendar.getInstance();
    
            if(month==1){
                cal.set(year,0,day);
            }else{
                cal.set(year,month-1,day);
            }
    
            int dow = cal.get(Calendar.DAY_OF_WEEK);
    
            switch (dow) {
            case 1:
                return "SUNDAY";
            case 2:
                return "MONDAY";
            case 3:
                return "TUESDAY";
            case 4:
                return "WEDNESDAY";
            case 5:
                return "THURSDAY";
            case 6:
                return "FRIDAY";
            case 7:
                return "SATURDAY";
            default:
                System.out.println("GO To Hell....");
            }
    
            return null;
        }
    
    0 讨论(0)
  • 2020-11-22 05:38
    import java.text.SimpleDateFormat;
    import java.util.Scanner;
    
    class DayFromDate {
    
        public static void main(String args[]) {
    
            System.out.println("Enter the date(dd/mm/yyyy):");
            Scanner scan = new Scanner(System.in);
            String Date = scan.nextLine();
    
            try {
                boolean dateValid = dateValidate(Date);
    
                if(dateValid == true) {
                    SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy" );  
                    java.util.Date date = df.parse( Date );   
                    df.applyPattern( "EEE" );  
                    String day= df.format( date ); 
    
                    if(day.compareTo("Sat") == 0 || day.compareTo("Sun") == 0) {
                        System.out.println(day + ": Weekend");
                    } else {
                        System.out.println(day + ": Weekday");
                    }
                } else {
                    System.out.println("Invalid Date!!!");
                }
            } catch(Exception e) {
                System.out.println("Invalid Date Formats!!!");
            }
         }
    
        static public boolean dateValidate(String d) {
    
            String dateArray[] = d.split("/");
            int day = Integer.parseInt(dateArray[0]);
            int month = Integer.parseInt(dateArray[1]);
            int year = Integer.parseInt(dateArray[2]);
            System.out.print(day + "\n" + month + "\n" + year + "\n");
            boolean leapYear = false;
    
            if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                leapYear = true;
            }
    
            if(year > 2099 || year < 1900)
                return false;
    
            if(month < 13) {
                if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                    if(day > 31)
                        return false;
                } else if(month == 4 || month == 6 || month == 9 || month == 11) {
                    if(day > 30)
                        return false;
                } else if(leapYear == true && month == 2) {
                    if(day > 29)
                        return false;
                } else if(leapYear == false && month == 2) {
                    if(day > 28)
                        return false;
                }
    
                return true;    
            } else return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:39

    Adding another way of doing it exactly what the OP asked for, without using latest inbuilt methods:

    public static String getDay(String inputDate) {
        String dayOfWeek = null;
        String[] days = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
            Date dt1 = format1.parse(inputDate);
            dayOfWeek = days[dt1.getDay() - 1];
        } catch(Exception e) {
            System.out.println(e);
        }
    
        return dayOfWeek;
    }
    
    0 讨论(0)
  • 2020-11-22 05:40

    One line answer:

    return LocalDate.parse("06/02/2018",DateTimeFormatter.ofPattern("dd/MM/yyyy")).getDayOfWeek().name();
    

    Usage Example:

    public static String getDayOfWeek(String date){
      return LocalDate.parse(date, DateTimeFormatter.ofPattern("dd/MM/yyyy")).getDayOfWeek().name();
    }
    
    public static void callerMethod(){
       System.out.println(getDayOfWeek("06/02/2018")); //TUESDAY
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题