我如何获得一个简单的日期而不是公历?

How do I get a simple date instead of a gregorian calendar?

我正在创建一个预订系统,我正在尝试输入一个特定的日期。我没有得到我想要的简单格式 2015/12/14,而是得到了这个:

java.util.GregorianCalendar[time=1481500800000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=11,WEEK_OF_YEAR=50,WEEK_OF_MONTH=3,DAY_OF_MONTH=12,DAY_OF_YEAR=347,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]

我如何更改我的代码,以便它只将它保存为一个简单的日期而不是这个?因为我认为这是导致我的程序崩溃的原因。

我的ReservationDAOclass:

 package projects.dao.textdao;

 import java.io.BufferedReader;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.nio.file.Path;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import projects.Reservation;
 import projects.Customer;
 import restaurant.dao.ReservationDAO;


 public class TextReservationDAO extends ReservationDAO {

static final char DELIMITER = ':';

@Override
public List<Reservation> loadReservations(Path path, List<Customer> customer) {
    List<Reservation> reservations = new ArrayList<>();

    try (BufferedReader br = new BufferedReader(new FileReader(path.toString()))) {
        int reservationId, customerId;
        Calendar dateTime;

        String[] temp;
        String line = br.readLine();

        while (line != null) {
            Customer reservationCustomer = null;
            temp = line.split(Character.toString(DELIMITER));
            reservationId = Integer.parseInt(temp[0]);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            String dateInString = temp[1];
            Date date = null;
            try {
                date = sdf.parse(dateInString);
            } catch (ParseException ex) {
                Logger.getLogger(TextReservationDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            dateTime = Calendar.getInstance();
            dateTime.setTime(date);

            customerId = Integer.parseInt(temp[2]);
            for (Customer c : customer) {
                if (c.getCustomerId() == customerId) {
                    reservationCustomer = c;
                }
            }

            line = br.readLine();
            Reservation r = new Reservation(reservationId, dateTime, reservationCustomer);
            reservations.add(r);
        }
        br.close();
    } catch (IOException ex) {
        Logger.getLogger(TextReservationDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return reservations;
}

@Override
public void storeReservations(Path path, List<Reservation> reservations) {
    try (PrintWriter output = new PrintWriter(path.toFile())) {
        for (Reservation r : reservations) {
            output.println(toFileString(r));
        }
        output.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(TextReservationDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private String toFileString(Reservation r) {
    String s
            = Integer.toString(r.getReservationId()) + DELIMITER
            + r.getDateString() + DELIMITER
            + Integer.toString(r.getCustomer().getCustomerId()) + DELIMITER;
    return s;
}


 }

我的ReservationClass:

 package projects;

 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.HashSet;
 import java.util.Set;

 public class Reservation {

private int reservationId;
private Calendar dateTime;
private Customer customer;


private static int numberOfReservations = 0;

public Reservation() {
    this.reservationId = 0;
    this.dateTime = null;
     this.customer = null;


    numberOfReservations++;
}

public Reservation(int reservationId, Calendar dateTime, Customer customer) {
    this.reservationId = reservationId;
    this.dateTime = dateTime;
      this.customer = customer;

    numberOfReservations++;
}

public static int getNumberOfReservations() {
    return numberOfReservations;
}

public int getReservationId() {
    return reservationId;
}

public void setreservationId(int reservationId) {
    this.reservationId = reservationId;
}

public Calendar getDateTime() {
    return dateTime;
}

public void setDateTime(Calendar dateTime)
{
    this.dateTime = dateTime;
}

    public Customer getCustomer()
{
    return customer;
}

public void setCustomer(Customer customer)
{
    this.customer = customer;
}     



public String getDateString() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMM dd");
    String dateString = formatter.format(dateTime.getTime());
    return dateString;
}

@Override
public String toString() {
    return "Reservation id: " + getReservationId() + ", "
            + "Date/Time " + getDateTime() + "," +
    "customer: " + getCustomer();

}
 } 

您可以使用 SimpleDateFormat

打印出 Date 对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
sdf.format(new Date());

试试这个:

new Date().toString()

This get default date format