How to determine day of week by passing specific date?

前端 未结 25 1457
夕颜
夕颜 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:49

    Yes. Depending on your exact case:

    • You can use java.util.Calendar:

      Calendar c = Calendar.getInstance();
      c.setTime(yourDate);
      int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
      
    • if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1 for Sunday, see Calendar.SUNDAY), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version")

    • if you have your input as string, rather than Date, you should use SimpleDateFormat to parse it: new SimpleDateFormat("dd/M/yyyy").parse(dateString)

    • you can use joda-time's DateTime and call dateTime.dayOfWeek() and/or DateTimeFormat.

    • edit: since Java 8 you can now use java.time package instead of joda-time

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

    For Java 8 or Later, Localdate is preferable

    import java.time.LocalDate;
    
    public static String findDay(int month, int day, int year) {
    
        LocalDate localDate = LocalDate.of(year, month, day);
    
        java.time.DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        System.out.println(dayOfWeek);
    
        return dayOfWeek.toString();
    }
    

    Note : if input is String/User defined, then you should parse it into int.

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

    There is a challenge on hackerrank Java Date and Time

    personally, I prefer the LocalDate class.

    1. Import java.time.LocalDate
    2. Retrieve localDate by using "of" method which takes 3 arguments in "int" format.
    3. finally, get the name of that day using "getDayOfWeek" method.

    There is one video on this challenge.

    Java Date and Time Hackerrank solution

    I hope it will help :)

    0 讨论(0)
  • 2020-11-22 05:54
    ...
    import java.time.LocalDate;
    ...
    //String month = in.next();
    int mm = in.nextInt();
    //String day = in.next();
    int dd = in.nextInt();
    //String year = in.next();
    int yy = in.nextInt();
    in.close();
    LocalDate dt = LocalDate.of(yy, mm, dd);
    System.out.print(dt.getDayOfWeek());
    
    0 讨论(0)
  • 2020-11-22 05:54
    //to get day of any date
    
    import java.util.Scanner; 
    import java.util.Calendar; 
    import java.util.Date;
    
    public class Show {
    
        public static String getDay(String day,String month, String year){
    
    
                String input_date = month+"/"+day+"/"+year;
    
                Date now = new Date(input_date);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(now);
                int final_day = (calendar.get(Calendar.DAY_OF_WEEK));
    
                String finalDay[]={"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};
    
                System.out.println(finalDay[final_day-1]);
    
        }
    
        public static void main(String[] args) { 
                Scanner in = new Scanner(System.in); 
                String month = in.next(); 
            String day = in.next();
                String year = in.next();
    
                getDay(day, month, year);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:55

    Simply use SimpleDateFormat.

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", java.util.Locale.ENGLISH);
    Date myDate = sdf.parse("28/12/2013");
    sdf.applyPattern("EEE, d MMM yyyy");
    String sMyDate = sdf.format(myDate);
    

    The result is: Sat, 28 Dec 2013

    The default constructor is taking "the default" Locale, so be careful using it when you need a specific pattern.

    public SimpleDateFormat(String pattern) {
        this(pattern, Locale.getDefault(Locale.Category.FORMAT));
    }
    
    0 讨论(0)
提交回复
热议问题