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

@ -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();
}
//-----------------------------------------------------------------------