Created
January 12, 2017 19:33
-
-
Save nakaly/c812213c548ac788dbbe8ff734e5ef6a to your computer and use it in GitHub Desktop.
LocalTime.toString()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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