-2

I am trying to read rows of a csv file. Each row represents a log history with timestamp at column 1. The timestamp is always in the form of YYYYMMDDHHMMSSTTT (to the millisecond).

However, I do not know how to parse time in the format of YYYYMMDDHHMMSSTTTand make it become an instance variable of a class that I am creating. In fact, I am not sure what type it should be in this case.

So, how should I go about parse time in the format of YYYYMMDDHHMMSSTTT,i.e. 20180301093559127? Thanks.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user1769197
  • 2,132
  • 5
  • 18
  • 32
  • 1
    Just be wary that MM != mm etc – achAmháin Apr 16 '18 at 17:36
  • 8
    `System.out.println(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").parse("20180301093559127"));` – Elliott Frisch Apr 16 '18 at 17:39
  • What did your search and research bring up? Surely you can find some information out there, both on a type to use and how to parse a date and time. The obvious Java type to use would be `LocalDateTime`, however if you know the time zone there may be advantages of using `ZonedDateTime`. – Ole V.V. Apr 17 '18 at 12:43

1 Answers1

-1

You can do something like this as a workaround. Though not a great solution, but will work for your case.

Assume your date is : String d="20180416235441095";

    String year=d.substring(0,4);
    String mon=d.substring(4,6);
    String day=d.substring(6,8);
    String hour=d.substring(8,10);
    String min=d.substring(10,12);
    String sec=d.substring(12,14);
    String mill=d.substring(14,17);

    String formattedDate=year+"-"+mon+"-"+day+" "+hour+":"+min+":"+sec+"."+mill;
    System.out.println("Formatted Date is : "+ formattedDate);
    LocalDateTime datetime = LocalDateTime.parse(formattedDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
    System.out.println("Final Date is : "+datetime);

Output is like: Formatted Date is : 2018-04-16 23:54:41.095 Final Date is : 2018-04-16T23:54:41.095

  • That won't work as the format that you are getting is not standard format of LocalDateTime and thats where you need to format your csv data in the format which can be parsed by this. Hope it helps! – Tamanna Bhasin Apr 16 '18 at 19:14