get Hour from long value - Java java.util

Java examples for java.util:Hour

Description

get Hour from long value

Demo Code


//package com.java2s;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        long time = 2;
        System.out.println(getHour(time));
    }// w  ww . j av  a  2s.co  m

    public static int getHour(long time) {
        DateFormat format = new SimpleDateFormat("HH");
        String hour = format.format(new Date(time * 1000l));
        if (hour.substring(0, 1).equals("0")) {
            hour = hour.substring(1);
        }
        return Integer.parseInt(hour);
    }
}

Related Tutorials