将日期保存到序列化文件
Im trying to save two dates into a serialized . bin
file. I use the Calendar class
to get the current date then I add 30 days onto it. So I try to save two date variables fd
(First Date) and ed
(Expiration Date). If I change them to Strings
in the expiration_date_serial file they work, but when I try to save them as Date
they throw errors on these 2 lines:
我试图将两个日期保存为序列化。 bin文件。我使用Calendar类来获取当前日期,然后我将其添加30天。所以我尝试保存两个日期变量fd(First Date)和ed(Expiration Date)。如果我将它们更改为expiration_date_serial文件中的字符串,它们可以工作,但是当我尝试将它们保存为Date时,它们会在这两行上抛出错误:
exp_date.fd = current_formateddate;
exp_date.fd = current_formateddate;
exp_date.ed = formateddate;
exp_date.ed = formateddate;
Error: incompatible types: java.lang.String cannot be converted to java.util.Date
错误:不兼容的类型:java.lang.String无法转换为java.util.Date
Runnable Class:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class GetCurrentDate {
public static void main(String[] args) {
// get current date
DateFormat currentdateFormat = new SimpleDateFormat("MM/dd/yy");
Date current_date = new Date();
String current_formateddate = currentdateFormat.format(current_date);
System.out.println("Current date: " + (current_formateddate));
// ADD 30 days to the current date
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 30);
Date d = c.getTime();
String formateddate = dateFormat.format(d);
System.out.println("+ 30 days: " + formateddate);
// Serialization start
expiration_date_serial exp_date = new expiration_date_serial();
exp_date.fd = current_formateddate;
exp_date.ed = formateddate;
String fileName = "data.bin";
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream(fileName));
os.writeObject(exp_date);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done writing...");
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream(
fileName));
expiration_date_serial p = (expiration_date_serial) is.readObject();
System.out.println("First Date = " + p.fd +
" Expiration Date = " + p.ed);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Other class:
import java.io.Serializable;
import java.util.Date;
public class expiration_date_serial implements Serializable {
public Date fd;//First Date
public Date ed;//Expiration Date
}
2 个解决方案
#1
0
tl;dr
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.plusDays( 30 )
.toString()
Details
The Answer by Orlangure is correct, about assigning across types.
Orlangure的回答是正确的,关于跨类型的分配。
Also, other problems include:
此外,其他问题包括:
- Using old outmoded legacy classes for date-time handling.
- Poor choice of format for serialized date values.
- Incorrectly choosing to use a date-time class for a date-only value.
使用旧的过时遗留类进行日期时间处理。
序列化日期值的格式选择不佳。
错误地选择将日期时间类用于仅日期值。
java.time
The old date-time classes are poorly-designed, confusing, and troublesome. Avoid them. Now legacy, supplanted by the java.time classes.
旧的日期时间类设计糟糕,令人困惑,麻烦。避免他们。现在遗留下来,取而代之的是java.time类。
ISO 8601
The ISO 8601 standard defines textual formats for date-time values. These formats are unambiguous, intuitive across cultures, and practical. The standard format for a date-only value is YYYY-MM-DD such as 2016-10-23
.
ISO 8601标准定义了日期时间值的文本格式。这些格式在各种文化中都是明确的,直观的,实用的。仅日期值的标准格式为YYYY-MM-DD,例如2016-10-23。
The java.time classes use ISO 8601 by default when parsing and generating Strings to represent their date-time values.
在解析和生成字符串以表示其日期时间值时,java.time类在默认情况下使用ISO 8601。
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate类表示没有时间且没有时区的仅日期值。
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因地区而异。例如,法国巴黎午夜过后几分钟,在魁北克蒙特利尔的“昨天”仍然是新的一天。
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Strings
To generate an ISO 8601 compliant String, simply call toString
.
要生成符合ISO 8601标准的String,只需调用toString即可。
String output = today.toString(); // 2016-10-23
To parse, simply call parse
.
要解析,只需调用解析。
LocalDate ld = LocalDate.parse( "2016-10-23" );
Date math
You can add or subtract amounts of time. Just call the plus…
and minus…
methods.
您可以添加或减去一定的时间。只需调用plus ...和minus ...方法。
LocalDate thirtyDaysAgo = ld.minusDays( 30 );
LocalDate oneMonthAgo = ld.minusMonths( 1 );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,如java.util.Date,Calendar和SimpleDateFormat。
The Joda-Time project, now in maintenance mode, advises migration to java.time.
现在处于维护模式的Joda-Time项目建议迁移到java.time。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle教程。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310。
Where to obtain the java.time classes?
从哪里获取java.time类?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
带有捆绑实现的标准Java API的一部分。
Java 9增加了一些小功能和修复。
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use….
ThreeTenABP项目特别适用于Android的ThreeTen-Backport(如上所述)。
请参阅如何使用....
Java SE 8和SE 9及更高版本内置。带有捆绑实现的标准Java API的一部分。 Java 9增加了一些小功能和修复。
Java SE 6和SE 7大部分java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
Android ThreeTenABP项目专门针对Android调整ThreeTen-Backport(如上所述)。请参阅如何使用....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
ThreeTen-Extra项目使用其他类扩展了java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,如Interval,YearWeek,YearQuarter等。
更多相关文章
- 当月的开始和结束日期
- java 计算两个日期间的所有日期
- 如果服务器位于不同的位置,如何保存客户端机器时间
- java时间转换,long , string和date和时间戳的互转
- Java时区转换与时间格式
- 黑马程序员 Java中根据YYYY-MM-DD格式的日期计算为星期几的两种