Set the date by the method of day, month and year



  • I've retrieved a bunch of documentation and I've learned that it's best to use Calendar to work with the date. But the question arose is whether JAVA could ask a date directly without any change, something like that. myDate.set("28.12.2016"); Or a comma?

    Indicate to which side dig or dagger examples (namely the setting of the date and its extraction).

    P.S. I write a console application, the date entry is manually performed by the user.



  • I understand you need a method. https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,%20int,%20int)

        Calendar calendar = Calendar.getInstance();
        calendar.set(2016, 6, 10);
    

    Use the date to show. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

    For example:

            SimpleDateFormat dayFormat = new SimpleDateFormat("EEE, d MMMM", Locale.getDefault());
            String myString = dayFormat.format(calendar.getTime());
    

    Ask the right pattern to get the date off, in the example above, the result will be "All 12 June."

    And when you enter the user, nothing prevents you from minus one month.

    Comment from @zRrr:

    SimpleDateFormat can handle dates, like:

    Date date = new SimpleDateFormat( "dd.MM.yyyy" ).parse( "28.12.2016" ); 
    

    It's better to understand the user input if the format of the date in the row I know.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2