java SimpleDataFormat在windows上返回部分时间
我试图获取当前时间并将其附加到生成的文件名中 代码片段:java SimpleDataFormat在windows上返回部分时间,java,time-format,Java,Time Format,我试图获取当前时间并将其附加到生成的文件名中 代码片段: DateTimeFormatter dtf = DateTimeFormatter.ofPattern(app.values.getProperty("yyyy-MM-dd")); LocalDateTime now = LocalDateTime.now(); String date = dtf.format(now); Date today = Calendar.getInstance().getTime(); SimpleDateF
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(app.values.getProperty("yyyy-MM-dd"));
LocalDateTime now = LocalDateTime.now();
String date = dtf.format(now);
Date today = Calendar.getInstance().getTime();
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm");
String time = timeFormat.format(today);
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + date + "_"+time+".pdf");
这在linux上运行良好(返回“Receipt_2018-04-18_11:13”作为文件名),但当我在Windows7上运行jar时,它只提供
Receipt_2018-04-18_11
生成的文件已损坏且为空。我怎样才能解决这个问题
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm");
String time = timeFormat.format(today);
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + date + "_"+time+".pdf");
:
是非法的文件字符(至少在Windows下,可能在其他系统下)
有关详细信息,请查看,它包含无效文件/目录名称字符的列表
例如,可以将其更改为-
SimpleDateFormat timeFormat = new SimpleDateFormat("hh-mm");
String time = timeFormat.format(today);
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + date + "_"+time+".pdf");
或者使用SimpleDateFormat timeFormat=newsimpledateformat(“hhmm”)代码>取而代之
更新
显然,解决眼前的问题对某些人来说是不够的
java.util.Date
类通常被认为是不推荐使用的,您应该改用更新(并且改进)的java.time
API,例如
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_hh-mm");
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + formatter.format(LocalDateTime.now()) + ".pdf");
或者,如果您不能(使用Java 8+),可以使用许多可用库中的一个,包括Java.time
:
是非法的文件字符(至少在Windows下,可能在其他系统下)
有关详细信息,请查看,它包含无效文件/目录名称字符的列表
例如,可以将其更改为-
SimpleDateFormat timeFormat = new SimpleDateFormat("hh-mm");
String time = timeFormat.format(today);
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + date + "_"+time+".pdf");
或者使用SimpleDateFormat timeFormat=newsimpledateformat(“hhmm”)代码>取而代之
更新
显然,解决眼前的问题对某些人来说是不够的
java.util.Date
类通常被认为是不推荐使用的,您应该改用更新(并且改进)的java.time
API,例如
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_hh-mm");
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + formatter.format(LocalDateTime.now()) + ".pdf");
或者,如果您不能(使用Java 8+),许多可用库中的一个,包括Java.time
的后端口,问题是“:”对于Windows操作系统上的文件名来说是不正确的字符,因此需要替换它。基本上你的代码是多余的,你不需要Calendar
,因为LocalDateTime
已经有了时间戳部分
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd_hh_mm");
LocalDateTime now = LocalDateTime.now();
String dateTime = dtf.format(now);
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + dateTime + ".pdf");
希望有帮助 问题在于“:”是Windows操作系统上文件名的不正确字符,因此需要替换它。基本上你的代码是多余的,你不需要Calendar
,因为LocalDateTime
已经有了时间戳部分
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd_hh_mm");
LocalDateTime now = LocalDateTime.now();
String dateTime = dtf.format(now);
FileOutputStream fs = new FileOutputStream("receipt/Receipt_" + dateTime + ".pdf");
希望有帮助 试试这个,我想你会喜欢的
对于java.util.Date,只需创建一个新的日期()
试试这个,我想你会喜欢的
对于java.util.Date,只需创建一个新的日期()
:
是非法的文件字符有两个问题:由于您可以使用现代Java日期和时间类型(DateTimeFormatter
和LocalDateTime
),我建议您完全跳过老式且设计糟糕的类(date
、Calendar
和SimpleDateFormat
)。现代类具有您想要的所有功能。像你这样的混音只会混淆,并有可能导致错误的结果。(2) 小写的hh
表示上午或下午1点到12点之间的小时。由于您没有AM/PM标记,我想您需要一天中00到23小时的大写字母HH
。:
是非法文件字符两个问题:因为您可以使用现代Java日期和时间类型(DateTimeFormatter
和LocalDateTime
),我建议您完全跳过那些过时且设计糟糕的类(Date
、Calendar
和SimpleDateFormat
)。现代类具有您想要的所有功能。像你这样的混音只会混淆,并有可能导致错误的结果。(2) 小写的hh
表示上午或下午1点到12点之间的小时。由于您没有AM/PM标记,我认为您需要在一天中的小时00到23之间使用大写字母HH
。如果有人已经在使用更好的DateTimeFormatter
@DavidConrad同意,告诉他们使用旧的和破损的(有状态的)SimpleDateFormat
,可能不是一个好建议,但是我把重点放在了手头上的主要问题上。如果有人已经在使用更好的DateTimeFormatter
@DavidConrad同意,告诉他们使用旧的和坏的(有状态的)SimpleDateFormat
,这可能不是一个好建议,但我把重点放在了手头上的主要问题上