-2
package assignment1;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Scanner;

public class Assignment1 {

    public static void main(String[] args) throws ParseException {
 
        Scanner input = new Scanner(System.in);
        SimpleDateFormat SDformat = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println("Enter Date (Month/Day/Year) :");
        String dinput = input.nextLine();
        Date date1 = null;
       
        if (dinput != null && dinput.trim().length() > 0)
            date1 = SDformat.parse(dinput);
       
        System.out.println(date1);
    }  
}

the code it makes the user to put any random date , and it will give the day only without time or date just the day like (Thursday) like this only

input : 07/13/2021 Output : Tue Jul 13 00:00:00 EEST 2021

I want to display just the day like Tuesday .

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • As you know how to use a date formatter, can you think of how you might use one to print the day? – tgdavies Jul 13 '21 at 12:30
  • Please state your problem, what your code does, what it should. Give clear input and expected output examples. – zubergu Jul 13 '21 at 12:31
  • the code it makes the user to put any random date , and it will give the day only without time or date just the day like (Thursday) like this only – Yazan Yousef Jul 13 '21 at 12:34
  • 1
    @YazanYousef then put it nicely edited in your question, most of people will not read your comnments. – zubergu Jul 13 '21 at 12:36
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 13 '21 at 13:05
  • 1
    Under the linked original question there are many answers using java.time. I recommend that you skip the first three answers using old and problematic classes like `SimpleDateFormat` or `Calendar`. – Ole V.V. Jul 13 '21 at 13:09
  • 1
    Welcome to Stack Overflow. What did your search turn up? Please learn that you are supposed to search before asking a question here and when you post a question, tell us what your search brought up and specify how it fell short of solving your problem. It’s for your own sake since (1) you often find a better answer faster that way (2) it allows us to give preciser and more focused answers. It also tends to prevent or at least reduce the number of downvotes. – Ole V.V. Jul 13 '21 at 13:10

1 Answers1

3

You could use some class that really just takes a date, which java.util.Date does not (it is simply a wrapper around the number of milliseconds since the UNIX epoch).

Here's a small example that does not validate the input due to brevity:

public static void main(String[] args) {
    // initialize a Scanner
    Scanner input = new Scanner(System.in);
    // define a formatter for parsing input of the expected pattern
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu");
    // fire up a prompt
    System.out.println("Enter Date (MM/dd/yyyy):");
    // store the line typed in by the user
    String dinput = input.nextLine();
    // parse it
    LocalDate localDate = LocalDate.parse(dinput, dtf);
    // and print some sentence as result
    System.out.println(String.format("The date %s was a %s", 
            localDate.format(DateTimeFormatter.ISO_DATE),
            localDate.getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE,
                                                    Locale.ENGLISH)));
    // or an alternative (this time just the day of week, but in a different way)
    System.out.println(localDate.format(
                                DateTimeFormatter.ofPattern("EEEE",
                                                            Locale.ENGLISH)));
}

Entering 11/11/1988, for example, would print this:

Enter Date (MM/dd/yyyy):
11/11/1988
The date 1988-11-11 was a Friday
Friday
deHaar
  • 17,687
  • 10
  • 38
  • 51