How to Format Minutes Using Java SimpleDateFormat


In this example we will show how to format minutes with Java SimpleDateFormat class. In programming, minutes are often formatted with either “m” or “mm” formats. If “mm” format is used, the number of minutes will be shown as digits in tens.  “0” will be automatically added at the beginning when the number of minutes is less than 10.

Source Code

package com.beginner.examples;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FormattingMinute {

	public static void main(String[] args) {
		
		//create Date.
		Date date = new Date();
		
		System.out.println("Before formatting , date is : " + date);
		
		/*
		 * To format minutes in m format use SimpleDateFormat("m") Constructor.
		 */
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("m");
		System.out.println("Minutes in m format : " + simpleDateFormat1.format(date));
		
		/*
		 * To format minutes in mm format use SimpleDateFormat("mm") Constructor.
		 */
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("mm");
		System.out.println("Minutes in mm format : " + simpleDateFormat2.format(date));

	}

}

Output:

Before formatting , date is : Fri Jun 14 14:05:39 CST 2019
Minutes in m format : 5
Minutes in mm format : 05

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments