java timestamp format - avoiding substring IndexException in a one liner solution

java timestamp example
timestamp datatype in java example
java timestamp format milliseconds
java timestamp to string
java timestamp format with timezone
java timestamp milliseconds
java.sql.timestamp timezone
java timestamp to date

I'm using the below code to get the timestamp in yyyy-MM-dd hh:mm:ss format.

val load_date ="T", " ").substring(0,19)

But sometimes, the substring throws Exception like below.

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 19
at java.lang.String.substring(

I have constraints to get this as a one liner solution.

Conversion of String to Date in Java [duplicate], How to convert simple String into local DateTime format in java [duplicate] java timestamp format - avoiding substring IndexException in a one liner solution. I'm using the below code to get the timestamp in yyyy-MM-dd hh:mm:ss format. val load_date ="T", " ").substring(0,19) But sometimes, the substring t

You can also get this format by using SimpleDateFormat

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String strDate = sdfDate.format(now);

Timestamp (Java Platform SE 8 ), Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. A Timestamp also provides formatting and parsing operations to support the JDBC since January 1, 1970, 00:00:00 GMT represented by this Timestamp object. Instant can store points on the time-line further in the future and further in the  Questions: I am storing time in a MySQL database as a Unix timestamp and that gets sent to some JavaScript code. How would I get just the time out of it? For example, in HH/MM/SS format. Answers: // Create a new JavaScript Date object based on the timestamp // multiplied by 1000 so that the

Please use DateTimeFormatter class to do this.

public static void main(String[] args) {
        System.out.println("yyyy-MM-dd HH:mm:ss")));

For more information on the patterns supported by DateTimeFormatter please follow this link ::

java - formato java timestamp, Utilize DateTimeFormatter classe para fazer isso. public static void main(String[] args) { System.out.println( Java 8 and later comes with the new java.time package built-in. See Tutorial. Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP. Similar to Joda-Time, java.time offers a LocalDate class to represent a date-only value without time-of-day and without time zone.

java - formato java timestamp, 1. Por favor, use DateTimeFormatter clase para hacer esto. public static void ://​ En aras de la exhaustividad, a partir de los documentos de String.substring(int, int)  Packaged Solution: Luxon. If you want to use a one solution to fit all, I highly recommend using Luxon (a modernized version of Moment.js) which also does formatting in many locales/languages and tons of other features.

List is moving when I'm hover a tags, However when I hover one a tag(link) all the others moves for some reason and I can't find a solution java timestamp format - avoiding substring IndexException in a one liner solution; 9156 | Node: how to put objects as keys of hash object? see..the basic thing in "A.substring(0, 1).toUpperCase()+A.substring(1, A.length())"is that in 1st string 'hello' you have to convert 'h' as 'H' and rest of thing will be in lowercase thats why we have used "A.substring(0, 1).toUpperCase()"here it will start from 0 which is inclusive and 1 is exclusive so only 'h' is taken into consideration for converting in uppercase letter,now for

ORA-01861: literal does not match format string, The ORA-01861 is one of those easy errors that'll slow your heartbeat back down to A full date/timestamp/time zone format string would appear as such: When the error is triggered, return to the line that Oracle indicated as the point of the  note that the first version, the one ` window.crypto.getRandomValues, does not keep the Version 4 UUIDs format defined by RFC 4122. That is instead of xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx` it yields xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx . – humanityANDpeace

  • The easiest way to identify the problem would be to print out / log the former String ( This way you'll exactly know why the index 19 is out of range.
  • I can't reproduce your problem (see here), and String#substring is robust with regard to a ending index which is greater than the string. You'll see in the demo that I took a substring with (0, 30), and it still ran.
  • Anyway, the proper way to implement this would be to use a date formatter to format your date, instead of replacing characters and taking a substring.
  • The answers below may be more optimal than the OP, but I still don't see why the OP's code should every be failing. Perhaps someone can address this.
  • Should this question be closed? No. @TimBiegeleisen’s comment convinced me that it’s not a duplicate. And my own answer convinced me that it can be reproduced.
  • Nice catch on explaining the actual problem +1.
  • Hi Ole..Great!.. was this documented anywhere?. Thanks a lot for saving my time.. my friend suggested below workaround. Do you see any issue with it.. or should I go with DateTimeFormatter val load_date ="T"," ")
  • For the documentation, see my edit, @stack0114106. Your friend’s workaround will also sometimes produce a string of length 16 (not 19). If that’s OK, I think the workaround is OK.
  • Thanks Ole.. I just changed the code to use DateTimeFormatter val load_date ="yyyy-MM-dd HH:mm:SS"))
  • You want lowercase ss for seconds in your format pattern string. With uppercase SS I just got 2018-09-18 14:06:87 when the time was 2018-09-18T14:06:43.878528 because SS is for fraction of second.
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.
  • Almost correct. Please watch the case of your format pattern letters (there is a difference between yyyy and YYYY, etc.). Using DateTimeFormatter is certainly the right way to go.
  • @OleV.V. thanks for pointing it out to me... i went through this answer and understood the difference better.
  • Thanks for receiving my critical comment so well. I still believe that uppercase SS is incorrect…