update string to LocalDateTime converting function

This commit is contained in:
wyq 2021-08-30 23:18:26 +08:00
parent 4bef547cd2
commit 3fecf63dfc
3 changed files with 52 additions and 5 deletions

View File

@ -480,8 +480,41 @@ public class JDateUtil {
* @return LocalDateTime
*/
public static LocalDateTime parseDateTime(String dtStr, DateTimeFormatter formatter) {
TemporalAccessor parsed = formatter.parse(dtStr);
LocalDate datePart;
LocalTime timePart;
ZoneOffset zoneOffset;
long epochDay = 0, nanoSeconds = 0;
int offsetSeconds = 0;
// get different parsed parts
if (parsed.isSupported(ChronoField.EPOCH_DAY)) {
epochDay = parsed.getLong(ChronoField.EPOCH_DAY);
}
if (parsed.isSupported(ChronoField.NANO_OF_DAY)) {
nanoSeconds = parsed.getLong(ChronoField.NANO_OF_DAY);
}
if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) {
offsetSeconds = parsed.get(ChronoField.OFFSET_SECONDS);
}
zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds);
datePart = LocalDate.ofEpochDay(epochDay);
timePart = LocalTime.ofNanoOfDay(nanoSeconds);
return OffsetDateTime.of(datePart, timePart, zoneOffset).toLocalDateTime();
}
/**
* Parse string to LocalDateTime
* @param dtStr The string
* @param formatter DateTimeFormatter
* @return LocalDateTime
*/
public static LocalDateTime parseDateTime_bak(String dtStr, DateTimeFormatter formatter) {
TemporalAccessor ta = formatter.parse(dtStr);
if (ta.isSupported(ChronoField.HOUR_OF_DAY))
return LocalDateTime.from(ta);
else {
if (ta.isSupported(ChronoField.DAY_OF_MONTH))

View File

@ -12,6 +12,7 @@ import org.meteoinfo.ndarray.DataType;
import org.meteoinfo.ndarray.util.DataTypeUtil;
import java.text.DecimalFormat;
import java.time.format.DateTimeFormatter;
/**
*
@ -22,6 +23,7 @@ public class Column {
protected String name;
protected DataType dataType;
protected String format;
protected DateTimeFormatter dateTimeFormatter;
protected int formatLen;
// </editor-fold>
// <editor-fold desc="Constructor">
@ -106,6 +108,9 @@ public class Column {
*/
public void setFormat(String value){
this.format = value;
if (this.dataType == DataType.DATE) {
this.dateTimeFormatter = DateTimeFormatter.ofPattern(value);
}
}
/**
@ -249,8 +254,15 @@ public class Column {
public String toString(Object o){
if (format == null)
return o.toString();
else
return String.format(format, o);
else {
if (this.dataType == DataType.DATE) {
if (this.dateTimeFormatter == null) {
this.dateTimeFormatter = DateTimeFormatter.ofPattern(this.format);
}
return ((LocalDateTime) o).format(this.dateTimeFormatter);
} else
return String.format(format, o);
}
}
/**

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\array">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/>
<Path OpenPath="D:\Working\MIScript\Jython\mis\dataframe">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\wind"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart\subplot"/>
@ -11,20 +10,23 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\maskout"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\ascii"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\patch"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf_sombrero.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\patch\polygon.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\dataframe\read_aaot.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf_sombrero.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\patch\polygon.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\dataframe\read_aaot.py"/>
</RecentFiles>
</File>
<Font>