-1

I want to be able to enter a date as in the code below and as well as print the day in the following format: "Sun Jan 01 00:00:00 GMT 2017" I also want to save the day of the week in a string variable. I would be extremely grateful for anyone that could help with this. Many thanks in advance!

import java.util.*;
import java.text.*;

public class Run {
  public static void main(String args[]) {

    try {
      Scanner kb = new Scanner (System.in);
      System.out.println("Please enter day of deadline: ");
      String day = kb.nextLine();
      System.out.println("Please enter month of deadline: ");
      String month = kb.nextLine();
      System.out.println("Please enter year of deadline: ");
      String year = kb.nextLine();

      String complete = (""+day+month+year);

      Date date = new SimpleDateFormat ("ddMMyyyy" ).parse(complete);
      System.out.println(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
}
zhon
  • 1,610
  • 1
  • 22
  • 31

2 Answers2

2

Your code won't work. What if user enters 1, 1, 2017? Then you get 112017 and that won't parse correctly.

Best way is to use the Java 8 LocalDate:

LocalDate date = LocalDate.of(Integer.parseInt(year),
                              Integer.parseInt(month),
                              Integer.parseInt(day));
String dayOfWeek = date.getDayOfWeek()
                       .getDisplayName(TextStyle.SHORT_STANDALONE,
                                       Locale.getDefault());
System.out.println(dayOfWeek); // prints "Sun" for 1/1/2017 (English locale)
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

You could use the java.time API, which has superseded java.util.Date and related classes since Java 8 and added lots of functionality for date and time processing, including formatting. In your case:

Scanner kb = new Scanner(System.in);
System.out.println("Please enter day of deadline: ");
String day = kb.nextLine();
System.out.println("Please enter month of deadline: ");
String month = kb.nextLine();
System.out.println("Please enter year of deadline: ");
String year = kb.nextLine();

LocalDate date = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));

ZonedDateTime datetime = ZonedDateTime.of(date, LocalTime.of(0, 0, 0), ZoneId.of("GMT"));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy");
System.out.println(formatter.format(datetime));

To elaborate a bit on the classes used: In java.time there exist several classes to represent dates, distinguishing between date, date with time, and whether time zone information is included. In the above example, first of all, a java.time.LocalDate object is created from the day, month and year string. This object is then converted to a java.time.ZonedDateTime, adding time and time zone information (in this case 00:00:00 and GMT).

The date-time patterns used by java.time.format.DateTimeFormatter are documented in https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html. For an introduction to the java.time API and why it has been created to replace java.util.Date, see e.g. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Beethoven
  • 375
  • 1
  • 8
  • Good Answer. One suggested change: Use [`OffsetDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html) for a moment on the timeline with an assigned offset-from-UTC such as UTC (GMT). The `ZonedDateTime` class is for assigning a full time zone rather than a mere offset. A time zone is an offset *plus* a set of rules for handling anomalies such as Daylight Saving Time (DST). Also, there is a handy constant for UTC, `ZoneOffset.UTC`, rather than `ZoneId.of("GMT")`. – Basil Bourque Sep 05 '16 at 16:00