如何将字符串转换为LocalDate
?
我见过这样的例子:
LocalDate dt = new LocalDate("2005-11-12");
但我的字符串就像:
2005-nov-12
答案 0 :(得分:161)
从Java 1.8开始,您可以使用java.time类在没有额外库的情况下实现此目的。请参阅Tutorial。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
formatter = formatter.withLocale( putAppropriateLocaleHere ); // Locale specifies human language for translating, and cultural norms for lowercase/uppercase and abbreviations and such. Example: Locale.US or Locale.CANADA_FRENCH
LocalDate date = LocalDate.parse("2005-nov-12", formatter);
虽然语法几乎相同。
答案 1 :(得分:68)
当您使用Joda Time时,您应该使用DateTimeFormatter
:
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
final LocalDate dt = dtf.parseLocalDate(yourinput);
如果使用Java 8或更高版本,请参阅hertzi's answer
答案 2 :(得分:13)
您可能必须从DateTime转到LocalDate。
DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MMM-dd");
DateTime dateTime = FORMATTER.parseDateTime("2005-nov-12");
LocalDate localDate = dateTime.toLocalDate();
答案 3 :(得分:8)
日期时间格式由org.joda.time.format.DateTimeFormatter class
执行。三个类提供工厂方法来创建格式化程序,这是一个。其他人是ISODateTimeFormat
和DateTimeFormatterBuilder
。
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MMM-dd");
LocalDate lDate = new LocalDate().parse("2005-nov-12",format);
final org.joda.time.LocalDate class
是一个不可变的日期时间类,表示没有时区的日期 。如果 Chronology 也是如此,LocalDate
是线程安全且不可变的。提供的所有标准年表类都是线程安全且不可变的。
答案 4 :(得分:0)
onDrawEnd (event) {
this.sizeFeature = event.feature
this.$refs.setSizeDialog.open()
},
onSizeSet (title) {
this.sizeFeature.set('graphics', true)
this.sizeFeature.set('style', sizeStyleId)
this.sizeFeature.set('title', title)
this.setFeatureStyle(this.sizeFeature)
// Save graphics after the line with title was drawn
developedDocumentsApi.saveDrawingGraphics(this.document.id, this.updateGraphicsObjList())
}
setFeatureStyle (feature) {
const styleId = feature.get('style')
let style = null
switch (styleId) {
case 0: {
style = this.getRedPoint()
break
}
...
case 11: {
const title = feature.get('title')
style = this.getSizeStyle(feature, title)
break
}
}
feature.setStyle(style)
}
getSizeStyle (feature, title) {
const pointStyle = new Style({
image: new Circle({
radius: width * 2,
fill: new Fill({ color: 'black' }),
stroke: new Stroke({ color: 'black', width: width / 2 })
}),
zIndex: Infinity
})
const lineStyle = new Style({
stroke: new Stroke({ color: 'black', width: 2 }),
text: new Text({
font: '12px Calibri,sans-serif',
overflow: true,
placement: 'line',
textBaseline: 'bottom',
fill: new Fill({ color: 'black' }),
stroke: new Stroke({ color: '#fff', width: 3 })
})
})
const startStyle = lineStyle.clone()
const endStyle = lineStyle.clone()
const resolution = this.devDocMap.getView().getResolution()
const styles = [pointStyle]
const geometry = feature.getGeometry()
if (geometry.getType() === 'LineString') {
console.log('LineString')
lineStyle.getText().setText((feature.getGeometry().getLength() / 1000).toFixed())
styles.push(lineStyle)
const pixels = 10
const start = geometry.getFirstCoordinate()
startStyle.setGeometry(LineString([[start[0], start[1] - pixels * resolution], [start[0], start[1] + pixels * resolution]]))
styles.push(startStyle)
const end = geometry.getLastCoordinate()
endStyle.setGeometry(LineString([[end[0], end[1] - pixels * resolution], [end[0], end[1] + pixels * resolution]]))
styles.push(endStyle)
return styles
}
}
具有内置格式,可以直接用于解析字符序列。这是区分大小写的,但是Nov和Nov和
NOV无法正常工作:
DateTimeFormatter
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
try {
LocalDate datetime = LocalDate.parse(oldDate, pattern);
System.out.println(datetime);
} catch (DateTimeParseException e) {
// DateTimeParseException - Text '2019-nov-12' could not be parsed at index 5
// Exception handling message/mechanism/logging as per company standard
}
提供了创建格式化程序的自定义方法。由于不区分大小写,因此Nov,nov和NOV将被视为相同。
DateTimeFormatterBuilder