How to convert part of the day to Java?

I work with Excel format where

0.6549

It means that we make up 65.49% per day. How to transform it into a beautiful time, for example

3:30:42 PM

in java?

+4
source share
1 answer

Try the following:

public static void main(String[] args) throws Exception { DateFormat format = new SimpleDateFormat("HH:mm:ss a"); format.setTimeZone(TimeZone.getTimeZone("UTC")); for(String fracStr : args) { double frac = Double.parseDouble(fracStr); long day = (long) Math.floor(frac); frac = frac - day; Date time = new Date((long) ( 86400000l * frac)); System.out.println(fracStr+" -> "+format.format(time)); } } 

A quick note here - GMT time zone may not give you the expected results, but UTC will be. This is because Java views GMT as a valid abbreviation for Europe / London, and between 1968 and 1972, London was in constant summer light. So, midnight January 1, 1970 (Java era) is also 1:00 January 1, 1970 (GMT).

+4
source

Source: https://habr.com/ru/post/1347741/


All Articles