How to get hour and minute from current date time in java


This example will show to how to display hour and minute from the current date. by using Calender class and Formatter class

package com.rrtutors.lib;
import java.util.Calendar;
import java.util.Formatter;
public class Test {
    public static void main(String[] args) {
        Formatter fmt = new Formatter();
        Calendar cal = Calendar.getInstance();
        fmt = new Formatter();
        fmt.format("%tl:%tM", cal, cal);
        System.out.println(fmt);
    }
}

 

Output:

22:25

 

Example 2:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE));

    }
}

 

Output:

22:25
 


JAVA File IO examples

JAVA Date & Time Progamming Examples