1

Using Jdk8. I am trying to convert time actually just hours of the day (like 1130) into DateTime. I am trying it 2 ways as below none of them work correctly. 1130 gets converted into 11:00 and another 3:00 none of them are correct. Is there also a 3rd way naming my Time Zone.

Long ltime = Long.parseLong("1130");
   Long  seconds = (ltime/100)*60*60;
   LocalDateTime tsutcDttime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
    LocalDateTime lclDttime =   LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), ZoneId.systemDefault());
 System.out.println("ZoneId.systemDefault: " +ZoneId.systemDefault());
 System.out.println("UTC LocalDateTime : "+ tsutcDttime); 
 System.out.println("Sys Def LocalDateTime : "+ lclDttime); 

ZoneId.systemDefault: America/Los_Angeles

UTC LocalDateTime : 1970-01-01T11:00

Sys Def LocalDateTime : 1970-01-01T03:00

This problem got solved by below.

Part2 of this problem For some of my time components I have a Date and for some other I don't (but I have to convert everything into datetime anyway.

How do I add the actual date when I have it? I am not seeing a method LocalDate (date)? For others will simply let default to 1970 unless a better soln exists. The below just puts the 1970 date:

LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(secondsOfDay, 0, ZoneOffset.UTC); 

But when I have a date I would like to add it to secondsOfDay and create my DateTime- How. Like Date = Jan 1 2016 + secondsOfDay

Thanks

Sam-T
  • 1,877
  • 6
  • 23
  • 51

2 Answers2

2

Assuming the string "1130" means 11 hours and 30 minutes you need to do the conversion to seconds separately for the hours and minutes:

long ltime = Long.parseLong("1130");

long hoursAsSeconds = (ltime / 100) * 60 * 60;
long minsAsSeconds = (ltime % 100) * 60;

long secondsOfDay = hoursAsSeconds + minsAsSeconds;

You can then use LocalTime to get the local time given the seconds:

LocalTime localTime = LocalTime.ofSecondOfDay(secondsOfDay);

You could also get the local time directly by parsing the time like this:

LocalTime localTime = LocalTime.parse("1130", DateTimeFormatter.ofPattern("HHmm"));

From that you can get a LocalDateTime using something like:

LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), localTime);

Or a ZonedDateTime using:

ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDate.now(), localTime, ZoneId.of("America/Los_Angeles"));
greg-449
  • 109,219
  • 232
  • 102
  • 145
1

This expression is causing the issue:

 Long  seconds = (ltime/100)*60*60;

Division operator returns integer value so you are loosing precision here.

1130/100 = 11.3 ~ 11
11 * 60*60 = 39600
11.3 * 60*60 = 40680

Division should be the last operation in your expression:

Long  seconds = (ltime*60*60)/100;
Dominik Kunicki
  • 1,037
  • 1
  • 12
  • 35
  • The "1130" string is presumably 11 hours and 30 minutes, this code treats the value as 1130 / 100 hours which is not the same. – greg-449 Nov 29 '16 at 16:16
  • @all Sorry for my goofy code, what was I thinking. Yes the solutions suggested will work. @greg-449 `LocalTime localTime = LocalTime.parse("1130", DateTimeFormatter.ofPattern("HHmm"));` this is the exactly the elegant solution I was looking for - I knew something like this should exist. I have Part2 of this problem for some of my time components I have a Date and for some other I don't (but I have to convert everything into datetime anyway. How do I add the actual date when I have it? I am not seeing a method LocalDate (date)? For others will simply let default to 1970 unless a soln exists. – Sam-T Nov 29 '16 at 18:16
  • @Sam-T Sorry but I don't get what you are asking. Ask a new question with more details. – greg-449 Nov 29 '16 at 18:19
  • @greg-449 Let me add details in the main question. – Sam-T Nov 29 '16 at 18:21
  • Added new question [link] http://stackoverflow.com/questions/40874557/java-how-to-add-time-and-date – Sam-T Nov 29 '16 at 19:49