32
32
import java .time .format .ResolverStyle ;
33
33
import java .time .temporal .ChronoField ;
34
34
import java .util .Base64 ;
35
+ import java .util .TimeZone ;
35
36
36
37
import org .apache .commons .codec .binary .Hex ;
37
38
@@ -522,10 +523,11 @@ private static class DateConverter implements OdpsObjectConverter {
522
523
parsePattern = DEFAULT_PARSE_PATTERN ;
523
524
}
524
525
if (!strictMode ) {
525
- legacyOutputFormatter = getLegacyDateTimeFormatter (outputPattern , OdpsType .DATE );
526
+ legacyOutputFormatter =
527
+ getLegacyDateTimeFormatter (outputPattern , config .timezone , OdpsType .DATE );
526
528
}
527
- outputFormatter = getDateTimeFormatter (outputPattern , null , OdpsType .DATE );
528
- parseFormatter = getDateTimeFormatter (parsePattern , null , OdpsType .DATE );
529
+ outputFormatter = getDateTimeFormatter (outputPattern , config . timezone , OdpsType .DATE );
530
+ parseFormatter = getDateTimeFormatter (parsePattern , config . timezone , OdpsType .DATE );
529
531
}
530
532
531
533
@ Override
@@ -599,7 +601,7 @@ private static class DatetimeConverter implements OdpsObjectConverter {
599
601
parseFormatter = getDateTimeFormatter (parsePattern , config .timezone , OdpsType .DATETIME );
600
602
}
601
603
if (!strictMode ) {
602
- legacyOutputFormatter = getLegacyDateTimeFormatter (outputPattern , OdpsType .DATETIME );
604
+ legacyOutputFormatter = getLegacyDateTimeFormatter (outputPattern , config . timezone , OdpsType .DATETIME );
603
605
}
604
606
outputFormatter = getDateTimeFormatter (outputPattern , config .timezone , OdpsType .DATETIME );
605
607
}
@@ -662,17 +664,17 @@ private static abstract class AbstractTimestampFormatter implements OdpsObjectCo
662
664
663
665
private static class TimestampConverter extends AbstractTimestampFormatter {
664
666
private final boolean useSqlFormat ;
665
-
667
+ private final ZoneId timezone ;
666
668
TimestampConverter (OdpsRecordConverterBuilder .Config config ) {
667
669
this .useSqlFormat = config .useSqlFormat ;
670
+ this .timezone = config .timezone ;
668
671
String outputPattern = config .timestampOutputFormat ;
669
672
if (outputPattern != null ) {
670
673
outputFormatter =
671
674
getDateTimeFormatter (outputPattern , config .timezone , OdpsType .TIMESTAMP );
672
675
} else {
673
676
outputFormatter = outputFormatter .withZone (config .timezone );
674
677
}
675
-
676
678
String parsePattern = config .timestampParseFormat ;
677
679
if (parsePattern != null ) {
678
680
parseFormatter =
@@ -690,9 +692,7 @@ public String format(Object object, TypeInfo typeInfo, OdpsRecordConverter conve
690
692
formattedStr = outputFormatter .format (i );
691
693
} else if (object instanceof Timestamp ) {
692
694
Timestamp timestamp = (Timestamp ) object ;
693
- formattedStr =
694
- timestamp .getNanos () == 0 ? timestamp .toString ().substring (0 , 19 )
695
- : timestamp .toString ();
695
+ formattedStr = outputFormatter .format (timestamp .toInstant ().atZone (timezone ));
696
696
} else if (object instanceof LocalDateTime ) {
697
697
LocalDateTime localDateTime = (LocalDateTime ) object ;
698
698
formattedStr = localDateTime .format (outputFormatter );
@@ -732,11 +732,11 @@ private static class TimestampNtzConverter extends AbstractTimestampFormatter {
732
732
733
733
String outputPattern = config .timestampNtzOutputFormat ;
734
734
if (outputPattern != null ) {
735
- outputFormatter = getDateTimeFormatter (outputPattern , null , OdpsType .TIMESTAMP_NTZ );
735
+ outputFormatter = getDateTimeFormatter (outputPattern , ZoneId . of ( "UTC" ) , OdpsType .TIMESTAMP_NTZ );
736
736
}
737
737
String parsePattern = config .timestampNtzParseFormat ;
738
738
if (parsePattern != null ) {
739
- parseFormatter = getDateTimeFormatter (outputPattern , null , OdpsType .TIMESTAMP_NTZ );
739
+ parseFormatter = getDateTimeFormatter (outputPattern , ZoneId . of ( "UTC" ) , OdpsType .TIMESTAMP_NTZ );
740
740
}
741
741
}
742
742
@@ -747,10 +747,8 @@ public String format(Object object, TypeInfo typeInfo, OdpsRecordConverter conve
747
747
Instant i = (Instant ) object ;
748
748
formattedStr = i .atZone (zoneId ).format (outputFormatter );
749
749
} else if (object instanceof Timestamp ) {
750
- Timestamp timestamp = (Timestamp ) object ;
751
- formattedStr =
752
- timestamp .getNanos () == 0 ? timestamp .toString ().substring (0 , 19 )
753
- : timestamp .toString ();
750
+ Instant i = ((Timestamp ) object ).toInstant ();
751
+ formattedStr = i .atZone (zoneId ).format (outputFormatter );
754
752
} else if (object instanceof LocalDateTime ) {
755
753
LocalDateTime localDateTime = (LocalDateTime ) object ;
756
754
formattedStr = localDateTime .format (outputFormatter );
@@ -872,12 +870,18 @@ private static DateTimeFormatter getDateTimeFormatter(String pattern, ZoneId zon
872
870
}
873
871
}
874
872
875
- private static SimpleDateFormat getLegacyDateTimeFormatter (String pattern , OdpsType odpsType ) {
873
+ private static SimpleDateFormat getLegacyDateTimeFormatter (String pattern , ZoneId zoneId ,
874
+ OdpsType odpsType ) {
876
875
try {
877
876
pattern = pattern .replace ("u" , "y" );
878
- return new SimpleDateFormat (pattern );
877
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat (pattern );
878
+ if (zoneId != null ) {
879
+ simpleDateFormat .setTimeZone (TimeZone .getTimeZone (zoneId ));
880
+ }
881
+ return simpleDateFormat ;
879
882
} catch (Exception e ) {
880
- throw new IllegalArgumentException ("DateTime format for " + odpsType + " illegal: " + pattern );
883
+ throw new IllegalArgumentException (
884
+ "DateTime format for " + odpsType + " illegal: " + pattern );
881
885
}
882
886
}
883
887
0 commit comments