Java FastDateFormat解析问题
我试图用Java FastDateFormat解析问题,java,datetime,Java,Datetime,我试图用FastDateFormat字符串“0759”和“1401”进行解析,但得到了parsingeexception 它在SimpleDataFormat中运行良好,但我想避免使用它,因为它不是线程安全的。我正在检查当前时间是否在采取其他行动的特定时间范围内。我想对下面的代码使用FastDateFormat。请告知 try{ SimpleDateFormat time_to_string = new SimpleDateFormat("HHmm"); String execu
FastDateFormat
字符串“0759”和“1401”进行解析,但得到了parsingeexception
它在SimpleDataFormat
中运行良好,但我想避免使用它,因为它不是线程安全的。我正在检查当前时间是否在采取其他行动的特定时间范围内。我想对下面的代码使用FastDateFormat
。请告知
try{
SimpleDateFormat time_to_string = new SimpleDateFormat("HHmm");
String executionEndTime = time_to_string.format(stepExecution.getEndTime());
Date emailDeliveryStartTime =
(Date) time_to_string.parse("0759");
Date emailDeliveryEndTime =
(Date) time_to_string.parse("1401");
Date currentSystemTime = (Date) time_to_string.parse(executionEndTime);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(emailDeliveryStartTime);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(emailDeliveryEndTime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(currentSystemTime);
Date isEmailTime = calendar3.getTime();
if (isEmailTime.after(calendar1.getTime()) && isEmailTime.before(calendar2.getTime())) {
//return flowexecutionstatus
}
} catch (ParseException ex) {
//log message
}
国家文件:
相当于DateFormat.parse(String)
有关详细信息,请参阅
现在,各国的文件:
解析给定字符串开头的文本以生成日期。
该方法不能使用给定字符串的整个文本
有关详细信息,请参见方法
日期解析
以及各国的文件:
此解析操作使用日历
生成日期作为一个
结果,日历的日期时间字段和时区值可能
已被覆盖,具体取决于子类实现。
Any
以前通过调用setTimeZone设置的时区值
可能需要恢复以进行进一步操作
因此,从上面我们可以看出,FastDateFormat.parse(String)
基本上使用了一个calendar
对象,然后覆盖该对象的日期时间字段,然后从中生成date
对象
但是,simpleDataFormat.parse(String)
的工作方式有细微的差别。没有覆盖parse(String)
方法,但是覆盖了parse(String,parseposition)
及其状态:
此解析操作使用日历生成日期所有的
日历的日期时间字段在解析之前被清除,并且
日历的日期时间字段的默认值用于任何
缺少日期时间信息。例如,数据的年份值
如果没有给出年份值,则解析的日期为1970年,带有GregorianCalendar
从解析操作。时区值可能会被覆盖,
取决于给定的模式和文本中的时区值。任何
以前通过调用setTimeZone设置的时区值
可能需要恢复以进行进一步操作
换句话说,我觉得SimpleDateFormat.parse(String)
能够提供所需的结果,因为它用缺省值替换缺少的字段,其中asFastDateFormat.parse(String)
无法解析,因为您提供的日期时间字符串中缺少无法替换的字段
最后,尽管您没有说明如何实例化FastDateFormat
变量。如果您已实例化为:
FastDateFormat time_to_string = FastDateFormat.getInstance();
那么我建议您使用以下方法:
FastDateFormat time_to_string = FastDateFormat.getInstance("HHmm");
stepExecution.getEndTime()返回的数据类型是什么?这是一个Spring批处理作业。因此,stepExecution有一个getEndTime()方法,它告诉您批处理作业完成运行的时间。如果是这样,为什么要将时间转换为字符串,然后再转换回时间?这样可以将我转换为正确的格式。至少这是我的思考过程,可能是错误的,但除此之外,转换成FastDateFormat是我真正需要帮助的。