java.time.format.DateTimeParseException:无法在索引0处分析文本
我有一个groovy代码,它给了我一个错误,我不明白为什么:java.time.format.DateTimeParseException:无法在索引0处分析文本,java,groovy,java-time,Java,Groovy,Java Time,我有一个groovy代码,它给了我一个错误,我不明白为什么: import java.text.SimpleDateFormat import java.time.DayOfWeek import java.time.LocalDate import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter import java.time.temporal.Temporal
import java.text.SimpleDateFormat
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters
static String convertDateTimeString(String fromFormat, String toFormat, String dateString) {
DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern(fromFormat, Locale.GERMAN)
LocalDateTime localDateTime = LocalDateTime.parse(dateString, fromFormatter)
DateTimeFormatter toFormatter = DateTimeFormatter.ofPattern(toFormat, Locale.GERMAN)
localDateTime.format(toFormatter)
}
String date = convertDateTimeString( 'EEE, dd MMM yyyy HH:mm:ss z', 'yyyy', "Wed, 04 Feb 2015 10:12:34 UTC")
assert date == '2015'
错误istjava.time.format.DateTimeParseException:无法在索引0处分析文本“Wed,04 Feb 2015 10:12:34 UTC”
但是我检查了房间,一切看起来都很好
您能告诉我这里有什么问题吗?问题是您将格式化程序设置为对区域设置进行解析/格式化。德语
,但将其设置为“Wed,Feb 2015 10:12:34 UTC”
进行解析Wed
是星期三的英语,不是德语
要解决此问题,只需将Locale.derman
替换为Locale.ENGLISH
。另一个解决方案是区域设置的参数。我们需要使用德语中较短的工作日“So、Mo、Di、Mi、Do、Fr、Sa”,而不是“Wed”
String date = convertDateTimeString( "EEE, dd MMM yyyy HH:mm:ss z", "yyyy", "Mi, 04 Feb
2015 10:12:34 UTC");
//2015年产出该死,我知道这一定是个小小的愚蠢错误。。。有道理!谢谢!