Home > Blockchain >  How can i customize date format in Java given inputs
How can i customize date format in Java given inputs

Time:06-05

I want to write excel file date in format "dd/mm/yyyy hh:mm" an example of that is 12/03/2022 08:30

The problem is that i have all these values as parameters.. My fields stored in database are:

days : integer
month :  integer
year:integer
time :  float (example  08.00) 

The goal is that i retrieve all of them and i want to combine and have a result of that "dd/mm/yyyy hh:mm".

Could i combine them to create that date format? I ask that because if i write that in excel as string maybe that type will causes problems with excel graphs etc..

CodePudding user response:

Why don't you store a single long ("bigint") for the complete time? It is exact down to a millisecond and won't overflow in the next generations. You can get this time using System.currentTimeMillis() and use time classes like Date / LocalDateTime. They've got methods for everything.


With your approach:

hour:

  • Round your time towards 0.

minute:

  • Subtract hour from time.
    • Now you've got your minute as a value from 0 (inclusive) to 1 (exclusive).
  • Multiply it by 60 to get it as a value from 0 (inclusive) to 60 (exclusive).

So the result looks like this:

int hour = (int) time;
int minute = (int) (time - hour * 60)
String timeString = days   "/"   month   "/"   year   " "   hour   ":"   minute;
  •  Tags:  
  • java
  • Related