从数组列表中获取日期,然后通过最快的Java

时间:2016-11-06 21:20:32

标签: java string sorting date arraylist

我正在尝试从用户输入的数组列表中获取日期(dateTime),然后在最快的日期对其进行排序时,我听说过Java中的SimpleDateFormat实用程序/文本但我不知道不知道如何利用它。但我愿意使用你们可以帮助我的任何方法,并且解释会很棒!

package ticketSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Ticket extends User {

    Scanner sc = new Scanner(System.in);
    static ArrayList<Ticket> ticketList = new ArrayList<Ticket>();
    String pri = "";
    String des = "";
    int rm = 0;
    private String dateTime;

    public Ticket(String userName) {
        super(userName);

    }

    public String getDateTime() {
        return dateTime;
    }

    public void menu2() {
        typesOfTicks();
    }

    public void typesOfTicks() {
        System.out.println("\nWhat type of ticket would you like to create?");
        System.out.println("1: Room Ticket. ");
        System.out.println("2: Common Area. ");
    }

    public void addTicket(Ticket ticket) {
        ticketList.add(ticket);
    }

    public void createRoomTicket() {
        System.out.println("You have created a new room ticket!");
        System.out.println("Date for maintence: ");
        dateTime = sc.nextLine();
        System.out.println("What is the room number: ");
        rm = Integer.parseInt(sc.nextLine());
        System.out.println("What is its priority (1 being the lowest, and 5 the highest)?");
        pri = sc.nextLine();
        System.out.println("Description: ");
        des = sc.nextLine();
        Ticket ticket = new Ticket("Date for maintence(12/34/56): " + dateTime + "\nRoom Number: " + rm + "\nPriority: " + pri + "\nDecription:  " + des + "\n");
        addTicket(ticket);
    }

    public void createAreaTicket() {
        String area = "";
        System.out.println("Date for maintence: ");
        dateTime = sc.nextLine();
        System.out.println("What area is the ticket for? (N, S, W, E)");

        boolean valid = false;
        while (!valid) {
            String s = sc.nextLine();
            if (s.equals("N")) {
                area = "North";
                valid = true;
            } else if (s.equals("S")) {
                area = "South";
                valid = true;
            } else if (s.equals("W")) {
                area = "West";
                valid = true;
            } else if (s.equals("E")) {
                area = "East";
                valid = true;
            } else {
                System.out.println("Not a valid Area. \nTry again: ");
            }

        }

        System.out.println("What's the priority? ");
        pri = sc.nextLine();
        System.out.println("Description: ");
        des = sc.nextLine();
        Ticket ticket = new Ticket("Date for maintence: " + dateTime + "\nArea: " + area + "\nPriority: " + pri + "\nDecription:  " + des + "\n");
        addTicket(ticket);
    }

    public int getTicket() {
        return ticketList.size();
    }


    void viewTicks() {
        if (ticketList.size() <= 0) {
            System.out.println("There are no tickets. ");
        }
        for (int i = 0; i < ticketList.size(); i++) {
            System.out.println(ticketList.get(i));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你有几个选择,但我会给你两个。在这两种情况下,我都会使用Date(或类似)对象来表示日期而不是String

  1. Ticket实施Comparable<T>界面并使用Collections.sort(List<T> list)
  2. 通过Collections.sort(List<T> list, Comparator<? super T> c)实施List,在Ticket上致电Comparator<T>
  3. 选项1:

    Comparable<Ticket>实施List,这样就可以通过调用Collections.sort(nameOfYourList)Ticket进行排序。

    选项1的示例package ticketsystem; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Ticket implements Comparable<Ticket> { private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private Date date; public Ticket(String d) throws ParseException { date = DATE_FORMAT.parse(d); } public Date getDate() { return date; } public String getDateString() { return DATE_FORMAT.format(date); } @Override public int compareTo(Ticket t) { // Compare the parameter t's Date to this instances Date (reversed to order by soonest first). return t.date.compareTo(date); } } 类:

    package ticketsystem;
    
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Example {
    
        public static void main(String[] args) throws ParseException {
    
            List<Ticket> tickets = new ArrayList<>();
    
            tickets.add(new Ticket("1900-05-06"));
            tickets.add(new Ticket("2016-11-02"));
            tickets.add(new Ticket("1976-12-01"));
    
            Collections.sort(tickets);
    
            for (Ticket t : tickets) {
                System.out.println(t.getDateString());
            }
        }
    }
    

    选项1的示例用法

    Comparator

    选项2: 指定在致电Collections.sort(List<T> list, Comparator<? super E> c)(或myListName.sort(Comparator<? super E> c))时使用的Ticket

    选项2的示例Comparable类:

    在这种情况下无需实施package ticketsystem; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Ticket { private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private Date date; public Ticket(String d) throws ParseException { date = DATE_FORMAT.parse(d); } public Date getDate() { return date; } public String getDateString() { return DATE_FORMAT.format(date); } }

    package ticketsystem;
    
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Example {
    
        public static void main(String[] args) throws ParseException {
    
            List<Ticket> tickets = new ArrayList<>();
    
            tickets.add(new Ticket("1900-05-06"));
            tickets.add(new Ticket("2016-11-02"));
            tickets.add(new Ticket("1976-12-01"));
    
            Collections.sort(tickets, new Comparator<Ticket>() {    
                @Override
                public int compare(Ticket t1, Ticket t2) {
                    return t2.getDate().compareTo(t1.getDate());
                }
            });
    
            for (Ticket t : tickets) {
                System.out.println(t.getDateString());
            }
    
        }
    }
    

    选项2的示例用法:

    else if