Я создаю систему бронирования и пытаюсь ввести конкретную дату. Вместо того, чтобы получить простой формат 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]
Как я могу изменить свой код, чтобы он просто сохранял его как простую дату вместо этого? Потому что я думаю, что это то, что приводит к сбою моей программы.
Мой ReservationDAO
класс:
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;
}
}
Мой Reservation
Класс:
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();
}
}