英文:
How do I create and use DateFormat with a given date in another annotation?
问题
获取具有预计交货日期的货件的详细信息。还获取该货件可用的货件状态数量,即中间遍历的数量。编写一个程序来比较货件的最终状态上的预计日期和货件的实际预计日期,然后显示货件是否按时到达、提前或延迟。
public class ShipmentMain {
public static void main(String[] args) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println("输入货件详细信息:");
Scanner sc = new Scanner(System.in);
String userDetail = sc.nextLine();
String[] s = userDetail.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]);
}
}
}
}
示例输入和输出 1:
输入货件详细信息:
STAJU01,Hong Kong,Cochin,20-05-2017,karthick
我无法输入日期字段。请指导我如何输入。
英文:
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
得分: 3
你可以截取字符串的前两个字符并保存它,将值解析为整数或其他类型。然后,你可以去掉“-”字符并继续处理接下来的两个整数字符。最后,你需要去掉四位数字。之后,你可以将它轻松添加到你的日期并与其他日期进行比较(例如,使用Date.Format.SHORT)。
英文:
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)
答案2
得分: 0
这个答案旨在指导你避免代码问题并帮助你解决卡住的问题。
第一点是:使用 Scanner#nextLine
而不是 Scanner#next
,因为 Scanner#next
会在找到第一个空格后停止扫描,也就是说它只会扫描到输入中的 STAJU01,Hong,而不会扫描完整的输入,即 STAJU01,Hong Kong,Cochin,20-05-2017,karthick。
示例:
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: ");
// 使用 Scanner#nextLine 来扫描整行
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(",");
System.out.println(Arrays.toString(userDetailParts));
}
}
一个示例运行:
Enter the shipment details: STAJU01,Hong Kong,Cochin,20-05-2017,karthick
[STAJU01, Hong Kong, Cochin, 20-05-2017, karthick]
尝试在将 sc.nextLine()
更改为 sc.next()
后使用相同的输入运行此代码,你将了解到两者之间的区别。
在将输入拆分为数组之后,你可以使用它们的索引来访问元素,例如使用 userDetailParts[3]
,你可以访问日期字符串。你的代码中的 for
循环是不必要的。
第二点是:不要使用过时的日期时间 API,例如 java.util.Date
和 java.text.DateFormat
。相反,使用现代日期时间 API。可以从 教程:日期时间 中了解更多信息。
示例:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String args[]) {
// 给定日期字符串
String dateStr = "20-05-2017";
// 定义用于格式化的模式
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(dateStr, inputFormatter);
System.out.println(date);
// 现在,如果你想以其他格式打印日期,可以相应地定义格式,例如
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("EEEE MMMM dd, yyyy");
String formatted = outputFormatter.format(date);
System.out.println(formatted);
}
}
输出:
2017-05-20
Saturday May 20, 2017
然而,如果你仍然坚持使用旧的日期时间 API 和其格式化,可以像下面这样做:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String args[]) throws ParseException {
// 给定日期字符串
String dateStr = "20-05-2017";
// 定义用于格式化的模式
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(dateStr);
// 使用默认格式打印日期(即 Date#toString)
System.out.println(date);
// 使用你的自定义格式打印日期
System.out.println(sdf.format(date));
}
}
输出:
Sat May 20 00:00:00 BST 2017
20-05-2017
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论