Formats our own relative time format to hh:ss. - Java java.time

Java examples for java.time:Format

Description

Formats our own relative time format to hh:ss.

Demo Code


//package com.java2s;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;

public class Main {
    /**/*from  w w  w .  j  av a  2  s .  c om*/
     * Formats our own relative time format to hh:ss.
     *
     * @param relativeTime Seconds after midnight.
     * @return A String containing the formatted time.
     */
    public static String formatRelativeTime(int relativeTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");

        return formatter.format(Timestamp
                .valueOf(convertRelativeTime(relativeTime)));
    }

    /**
     * Converts our own relative time format to a LocalDateTime object.
     * The date part is the current date.
     *
     * @param relativeTime Seconds after midnight.
     * @return A LocalDateTime for today, at the given time.
     */
    public static LocalDateTime convertRelativeTime(int relativeTime) {
        return LocalDateTime.of(LocalDate.now(),
                LocalTime.ofSecondOfDay(relativeTime));
        // int hours = relativeTime/3600;
        // relativeTime -= (hours * 3600);
        // int minutes = relativeTime/60;
        // relativeTime -= (minutes * 60);
        // int seconds = relativeTime;

        // return LocalDate.now().atTime(hours, minutes, seconds);
    }
}

Related Tutorials