Trying to get date format in Java - calling from separate function

Related searches

I am trying to retrieve the date format depending on the month, day and year the user enters. It should be called from one function and the logic in another. When I run the code, the questions run but the date is not outputted even though it is being returned to the getArrivalDate method. Any ideas?

CODE:

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.Month;
import java.text.NumberFormat;
import java.time.LocalDate;

public class Reserve {

public static void main(String[] args) {


    System.out.println("Enter the requested input");
    System.out.println();

    //Call Arrival Date Method
    getArrivalDate();
}

 public static void getArrivalDate() {
    // create a Scanner object
        Scanner sc = new Scanner(System.in);
        String choice = "y";

        while (choice.equalsIgnoreCase("y")) {

            System.out.print("month (1-12): ");
            int month = sc.nextInt();
            System.out.print("day (1-31):  ");
            int day = sc.nextInt();
            System.out.print("year: ");
            int year = sc.nextInt();  

         setArrivalDate(choice);
          //Users choice
            choice = sc.nextLine();
        }
 } //End getArrivalDate

 public static String setArrivalDate(String arrivalDate1) {

        String month = "no month";
        int day = 0;
        int year = 0;

        switch(month) {
        case "1":
            month = "January";
        break;

        case "2":
            month = "February";
        break;

        case "3":
            month = "March";
        break;

        case "4":
            month = "April";
        break;

        case "5":
            month = "May";
        break;

        case "6":
            month = "June";
        break;

        case "7":
            month = "July";
        break;

        case "8":
            month = "August";
        break;

        case "9":
            month = "September";
        break;

        case "10":
            month = "October";
        break;

        case "11":
            month = "November";
        break;

        case "12":
            month = "December";
        break;
        }

 return arrivalDate1 = "Arrival Date: " + month + day + year;
      return arrivalDate1;  
}


}

You need to at least print out the return value

And you're passing in choice, which is always just "y"... If you want to show the actual date, start by passing in each value

System.out.println(setArrivalDate(year, month, day));

This isn't really correct either return arrivalDate1 = "Arrival Date: ".... You don't have to return a variable, just return the string like return "Arrival Date:"...

And rather than a switch case, I suggest learning about the DateFormatter class

How to format Date in Java, If you want to share SimpleDateFormat or want to make it thread-safe, you can Once you create a DateFormat, you can just call format() method which accept java.util. //Another Example of formatting Date in Java using SimpleDateFormat I suggest try following format "dd MMM yyyy HH:mm", while coverting String "25 � At the moment I am trying to store the date using a Date object. @Required public Date date; And the output of this object is something like: January 1, 1970, 00:00:00 GMT. What I would really like to do is separate this and store the date in a format like 27/02/2013 and have the time as a separate object in 24 hour format e.g. 23:45. I am

You are taking day, month and year as an input but not passing it to setArrivalDate() method. Even this method is returning some default value you are not printing anything. Here is a workable code:

import java.util.Scanner;

class Reserve {

public static void main(String[] args) {


    System.out.println("Enter the requested input");
    System.out.println();

    // Call Arrival Date Method
    getArrivalDate();
}

public static void getArrivalDate() {
    // Create a Scanner object
    Scanner sc = new Scanner(System.in);
    String choice = "y";

    while (choice.equalsIgnoreCase("y")) {

        System.out.print("month (1-12): ");
        int month = sc.nextInt();
        System.out.print("day (1-31):  ");
        int day = sc.nextInt();
        System.out.print("year: ");
        int year = sc.nextInt();

        // To remove a trailing '\n'.
        sc.nextLine();

        // You have to pass the values you got from user.
        System.out.println(setArrivalDate(day, month, year));

        // You should again dispaly a message to ask for input again.
        System.out.print("Enter 'Y/y' to continue:");

        //Users choice
        choice = sc.nextLine();
    }
} //End getArrivalDate

public static String setArrivalDate(int day, int month, int year) {
    String monthStr = "";
    switch (month) {
        case 1:
            monthStr = "January";
            break;

        case 2:
            monthStr = "February";
            break;

        case 3:
            monthStr = "March";
            break;

        case 4:
            monthStr = "April";
            break;

        case 5:
            monthStr = "May";
            break;

        case 6:
            monthStr = "June";
            break;

        case 7:
            monthStr = "July";
            break;

        case 8:
            monthStr = "August";
            break;

        case 9:
            monthStr = "September";
            break;

        case 10:
            monthStr = "October";
            break;

        case 11:
            monthStr = "November";
            break;

        case 12:
            monthStr = "December";
            break;
    }

    return "Arrival Date: " + monthStr + day + year;
}

}

There are lot of things you can handle. First if you setup a proper format you can use SimpleDateFormat and clean a code little bit. Next thing is that you should be careful when you use sc.nextLine() after sc.nextInt(). sc.nextInt() will take the integer value and leave a newline character behind which is then taken by sc.nextLine(). Because of this the actual string you wanted to capture is not read.

How to Parse Date in Desired format - Java Date, This post is to discuss few important points about parse() method. Converting strings to desired date format is a time consuming and tedious process in many languages including Java. DateFormat uses the standard date formatting of the current locale If this isn't what you want, call date. Try these related posts. Java SimpleDateFormat. The java.text.SimpleDateFormat class provides methods to format and parse date and time in java. The SimpleDateFormat is a concrete class for formatting and parsing date which inherits java.text.DateFormat class.

Try this:

import java.util.Scanner;

public class Reverse {

 public static void main(String[] args) {

    System.out.println("Enter the requested input");

    //Call Arrival Date Method
    getArrivalDate();

}


private static void getArrivalDate() {
    // create a Scanner object
    Scanner sc = new Scanner(System.in);
    String again;
    do {
        System.out.print("month (1-12): ");
        int month = sc.nextInt();
        while(month>12){
            System.out.print("Month must be between 1-12.\nTry again:");
            month=sc.nextInt();
        }
        System.out.print("day (1-31):  ");
        int day = sc.nextInt();
        System.out.print("year: ");
        int year = sc.nextInt();

        setArrivalDate(month, day, year);
        System.out.println("\nDo you want to continue? (Y/N): ");
        again = sc.next();
    } while (again.equalsIgnoreCase("Y"));

} //End getArrivalDate


private static void setArrivalDate(int month, int day, int year) {


    System.out.print("\nArrival Date: ");
    switch (month) {
        case 1:
            System.out.println("January " + day + " " + year);
            break;

        case 2:
            System.out.println("Feb " + day + " " + year);
            break;

        case 3:
            System.out.println("March " + day + " " + year);
            break;

        case 4:
            System.out.println("April " + day + " " + year);
            break;

        case 5:
            System.out.println("May " + day + " " + year);
            break;

        case 6:
            System.out.println("June " + day + " " + year);
            break;

        case 7:
            System.out.println("July " + day + " " + year);
            break;

        case 8:
            System.out.println("August " + day + " " + year);
            break;

        case 9:
            System.out.println("September " + day + " " + year);
            break;

        case 10:
            System.out.println("October " + day + " " + year);
            break;

        case 11:
            System.out.println("November " + day + " " + year);
            break;

        case 12:
            System.out.println("December " + day + " " + year);

    }
  }
}

Java Date and Time, Java does not have a built-in Date class, but we can import the java.time package to work with the LocalDate class, and use its now() method: Run example � The ofPattern() method accepts all sorts of values, if you want to display the date and time in a different format. E, MMM dd yyyy, "Thu, Sep 29 1988", Try it � � Learn to create new date, get current date, parse date to string or format Date object using java.util.Date class. These use-cases are frequently required, and having them in one place will help in saving time for many of us.

DateFormat (Java Platform SE 7 ), Your code can be completely independent of the locale conventions for months, To format a date for a different Locale, specify it in the call to getDateInstance() . Use getDateInstance to get the normal date format for that country. This will work for the majority of countries; just remember to put it in a try block in case you � Formatting Date and Time. The "T" in the example above is used to separate the date from the time. You can use the DateTimeFormatter class with the ofPattern() method in the same package to format or parse date-time objects. The following example will remove both the "T" and nanoseconds from the date-time:

How to format/parse dates with LocalDateTime in , One of the common tasks in Java project is formatting or parsing date Ideally, you can store the reference of DateTimeFormatter into a static variable to make it global. a separate formatter and you can directly call the parse method, in a static variable and share among whoever wants to use but make� Java SimpleDateFormat and DateFormat classes are used for date formatting. It is mostly used where we need to display or utilize the date and time functionality of Java. Both of these classes are present in com.text package. DateFormat is used for formatting a date into String based on specific locale that is provided as input.

In this short tutorial, we'll look at how to extract the year, month and day from a given Date in Java. We'll be looking at how to extract these values using the legacy java.util.Date class and also by using the new date-time library of Java 8.

Comments
  • setArrivalDate returns a value... You never capture it
  • How do I "capture" it? Sorry, new to this.
  • That code doesn't compile. Please try again. --- Why are you passing "y" (value of choice) into the setArrivalDate method? --- Why is setArrivalDate method not using the arrivalDate1 parameter for anything? --- Why do you expect switch("no month") (value of month) to match any of those case statements?
  • If I used the date formatter how would I apply it to my use case? Would I set it to it's own method? public String getCheckInDateFormatted(){ DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); return dtf.format(checkInDate); }}
  • You're trying to convert month numbers to valid strings. Therefore, you can construct one format, then you must parse to a date object, which would be formatted to another. Examples here stackoverflow.com/a/42240771/2308683
  • thank you for this! I also need to do a departure date...would I follow the same convention??