Skip to content

Instantly share code, notes, and snippets.

@nakaly
Created January 12, 2017 19:33
Show Gist options
  • Save nakaly/c812213c548ac788dbbe8ff734e5ef6a to your computer and use it in GitHub Desktop.
Save nakaly/c812213c548ac788dbbe8ff734e5ef6a to your computer and use it in GitHub Desktop.
LocalTime.toString()
public String toString() {
StringBuilder buf = new StringBuilder(18);
int hourValue = hour;
int minuteValue = minute;
int secondValue = second;
int nanoValue = nano;
buf.append(hourValue < 10 ? "0" : "").append(hourValue)
.append(minuteValue < 10 ? ":0" : ":").append(minuteValue);
if (secondValue > 0 || nanoValue > 0) {
buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
if (nanoValue > 0) {
buf.append('.');
if (nanoValue % 1000_000 == 0) {
buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
} else if (nanoValue % 1000 == 0) {
buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
} else {
buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
}
}
}
return buf.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment