问题
Get the details of a Shipment with the expected date of delivery. Also get the number of shipment status available for that shipment, meaning the number of intermediate traversals. Write a program to compare if the expected date on the final status of the shipment and the actual expected date of the Shipment and display whether the Shipment has arrived 'on time' or 'before' or 'after' the expected date.
public class ShipmentMain {
public static void main(String[] args) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println("Enter the shipment details :");
Scanner sc = new Scanner(System.in);
String userDeatil = sc.next();
String[] s = userDeatil.split(",");
Shipment shipment = new Shipment();
ShipmentStatus status = new ShipmentStatus();
for (int j = 0; j < s.length; j++) {
if (j == 0) {
shipment.setid(s[j]);
} else if (j == 1) {
shipment.setsourcePort(s[j]);
} else if (j == 2) {
shipment.setdestinationPort(s[j]);
} else if (j == 3) {
shipment.setexpectedDeliveryDate(s[j]);
} else if (j == 4) {
shipment.setcustomerName(s[j]);
}
}
}
}
Sample Input and Output 1:
Enter the shipment details :
STAJU01,Hong Kong,Cochin,20-05-2017,karthick
I am unable to input with the date field. Please guide me on how I can input this.
回答1:
This answer is meant to guide you to avoid the problems with your code and to help you solve the problem where you are stuck.
The first point is: Use Scanner#nextLine
instead of Scanner#next
as Scanner#next
will stop scanning after it finds the first space i.e. it will scan only up to STAJU01,Hong for the input, STAJU01,Hong Kong,Cochin,20-05-2017,karthick
Demo:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the shipment details: ");
// Use Scanner#nextLine to scan the full line
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(",");
System.out.println(Arrays.toString(userDetailParts));
}
}
A sample run:
Enter the shipment details: STAJU01,Hong Kong,Cochin,20-05-2017,karthick
[STAJU01, Hong Kong, Cochin, 20-05-2017, karthick]
Try running this code with the same input after changing sc.nextLine()
to sc.next()
and you will understand the difference.
After splitting the input into an array, you can access the elements using their indices e.g. by using userDetailParts[3]
, you can access the date string. The for
loop in your code is unnecessary.
The second point is: do not use the outdated date-time API e.g java.util.Date
and java.text.DateFormat
. Use the modern date-time API instead. Learn more about it from Trail: Date Time.
Demo:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String args[]) {
// Given date string
String dateStr = "20-05-2017";
// Define the pattern for formatting
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(dateStr, inputFormatter);
System.out.println(date);
// Now, if you want to print the date in some other format, you can define the
// format accordingly e.g.
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("EEEE MMMM dd, yyyy");
String formatted = outputFormatter.format(date);
System.out.println(formatted);
}
}
Output:
2017-05-20
Saturday May 20, 2017
However, if you still insist to use the legacy API for date-time and its formatting, you can do it as follows:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String args[]) throws ParseException {
// Given date string
String dateStr = "20-05-2017";
// Define the pattern for formatting
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(dateStr);
// Print date using the default format (i.e. Date#toString)
System.out.println(date);
// Print date using your custom format
System.out.println(sdf.format(date));
}
}
Output:
Sat May 20 00:00:00 BST 2017
20-05-2017
回答2:
You could cut the first two Characters off the String and save it, parse the Value into an int or something. Then you could cut the "-" character off and proceed with the next to int Chars. In the end you need to cut off four digits. After this you can easily add it to your Date and compare it to others. (For example Date.Format.SHORT)
来源:https://stackoverflow.com/questions/63264779/how-do-i-create-and-use-dateformat-with-a-given-date-in-another-annotation