Skip to content

Commit 449ca2c

Browse files
wenshaoliach
authored andcommittedAug 27, 2024
8337832: Optimize datetime toString
Reviewed-by: scolebourne, liach, naoto
1 parent b1b4cd4 commit 449ca2c

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed
 

‎src/java.base/share/classes/java/time/LocalDateTime.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1966,10 +1966,17 @@ public int hashCode() {
19661966
@Override
19671967
public String toString() {
19681968
var buf = new StringBuilder(29);
1969+
formatTo(buf);
1970+
return buf.toString();
1971+
}
1972+
1973+
/**
1974+
* Prints the toString result to the given buf, avoiding extra string allocations.
1975+
*/
1976+
void formatTo(StringBuilder buf) {
19691977
date.formatTo(buf);
19701978
buf.append('T');
19711979
time.formatTo(buf);
1972-
return buf.toString();
19731980
}
19741981

19751982
//-----------------------------------------------------------------------

‎src/java.base/share/classes/java/time/OffsetDateTime.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1923,7 +1923,10 @@ public int hashCode() {
19231923
*/
19241924
@Override
19251925
public String toString() {
1926-
return dateTime.toString() + offset.toString();
1926+
var offsetStr = offset.toString();
1927+
var buf = new StringBuilder(29 + offsetStr.length());
1928+
dateTime.formatTo(buf);
1929+
return buf.append(offsetStr).toString();
19271930
}
19281931

19291932
//-----------------------------------------------------------------------

‎src/java.base/share/classes/java/time/OffsetTime.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1398,7 +1398,10 @@ public int hashCode() {
13981398
*/
13991399
@Override
14001400
public String toString() {
1401-
return time.toString() + offset.toString();
1401+
var offsetStr = offset.toString();
1402+
var buf = new StringBuilder(18 + offsetStr.length());
1403+
time.formatTo(buf);
1404+
return buf.append(offsetStr).toString();
14021405
}
14031406

14041407
//-----------------------------------------------------------------------

‎src/java.base/share/classes/java/time/ZonedDateTime.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2214,11 +2214,20 @@ public int hashCode() {
22142214
*/
22152215
@Override // override for Javadoc
22162216
public String toString() {
2217-
String str = dateTime.toString() + offset.toString();
2217+
var offsetStr = offset.toString();
2218+
var zoneStr = (String) null;
2219+
int length = 29 + offsetStr.length();
22182220
if (offset != zone) {
2219-
str += '[' + zone.toString() + ']';
2221+
zoneStr = zone.toString();
2222+
length += zoneStr.length() + 2;
22202223
}
2221-
return str;
2224+
var buf = new StringBuilder(length);
2225+
dateTime.formatTo(buf);
2226+
buf.append(offsetStr);
2227+
if (zoneStr != null) {
2228+
buf.append('[').append(zoneStr).append(']');
2229+
}
2230+
return buf.toString();
22222231
}
22232232

22242233
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.