如何在另一个批注中给定日期创建和使用DateFormat? [英] How do I create and use DateFormat with a given date in another annotation?

查看:53
本文介绍了如何在另一个批注中给定日期创建和使用DateFormat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取带有预计交付日期的货件详细信息.还要获取可用于该货件的货件状态数量,即中间遍历的数量.编写一个程序,以比较货物最终状态的预期日期和实际的装运预期日期,并显示装运是否在预期时间准时",之前"或之后"到达.

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]);
            } 
        
        }
    }
}

示例输入和输出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.

推荐答案

此答案旨在指导您避免代码中的问题并帮助您解决问题所在.

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.

第一点是:使用 Scanner#nextLine 而不是 Scanner#next ,因为 Scanner#next 找到第一个后将停止扫描空格,即它将仅扫描最多 STAJU01,Hong 作为输入, STAJU01,Hong Kong,Cochin,20-05-2017,karthick

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

演示:

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));
    }
}

示例运行:

Enter the shipment details: STAJU01,Hong Kong,Cochin,20-05-2017,karthick
[STAJU01, Hong Kong, Cochin, 20-05-2017, karthick]

sc.nextLine()更改为 sc.next()后,尝试使用相同的输入运行此代码,您将了解其中的区别.

Try running this code with the same input after changing sc.nextLine() to sc.next() and you will understand the difference.

将输入拆分为数组后,您可以使用其索引访问元素,例如通过使用 userDetailParts [3] ,您可以访问日期字符串.您的代码中的 for 循环是不必要的.

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.

第二点是:不要使用过时的日期时间API,例如 java.util.Date java.text.DateFormat .改用现代日期时间API .从 跟踪:日期时间 了解更多信息strong>.

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.

演示:

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);
    }
}

输出:

2017-05-20
Saturday May 20, 2017

但是,如果您仍然坚持使用旧版API进行日期时间及其格式设置,则可以按以下步骤进行操作:

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));
    }
}

输出:

Sat May 20 00:00:00 BST 2017
20-05-2017

这篇关于如何在另一个批注中给定日期创建和使用DateFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆