8337832: Optimize datetime toString

Reviewed-by: scolebourne, liach, naoto
This commit is contained in:
Shaojin Wen 2024-08-27 22:10:05 +00:00 committed by Chen Liang
parent b1b4cd429a
commit 449ca2c3c1
4 changed files with 31 additions and 9 deletions

View File

@ -1966,10 +1966,17 @@ public final class LocalDateTime
@Override
public String toString() {
var buf = new StringBuilder(29);
formatTo(buf);
return buf.toString();
}
/**
* Prints the toString result to the given buf, avoiding extra string allocations.
*/
void formatTo(StringBuilder buf) {
date.formatTo(buf);
buf.append('T');
time.formatTo(buf);
return buf.toString();
}
//-----------------------------------------------------------------------

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1923,7 +1923,10 @@ public final class OffsetDateTime
*/
@Override
public String toString() {
return dateTime.toString() + offset.toString();
var offsetStr = offset.toString();
var buf = new StringBuilder(29 + offsetStr.length());
dateTime.formatTo(buf);
return buf.append(offsetStr).toString();
}
//-----------------------------------------------------------------------

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1398,7 +1398,10 @@ public final class OffsetTime
*/
@Override
public String toString() {
return time.toString() + offset.toString();
var offsetStr = offset.toString();
var buf = new StringBuilder(18 + offsetStr.length());
time.formatTo(buf);
return buf.append(offsetStr).toString();
}
//-----------------------------------------------------------------------

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -2214,11 +2214,20 @@ public final class ZonedDateTime
*/
@Override // override for Javadoc
public String toString() {
String str = dateTime.toString() + offset.toString();
var offsetStr = offset.toString();
var zoneStr = (String) null;
int length = 29 + offsetStr.length();
if (offset != zone) {
str += '[' + zone.toString() + ']';
zoneStr = zone.toString();
length += zoneStr.length() + 2;
}
return str;
var buf = new StringBuilder(length);
dateTime.formatTo(buf);
buf.append(offsetStr);
if (zoneStr != null) {
buf.append('[').append(zoneStr).append(']');
}
return buf.toString();
}
//-----------------------------------------------------------------------