How to Format Second Field Using Java SimpleDateFormat


Here we demonstrate how to format second field with SimpleDateFormat class in Java, and the Seconds may be formatted in either s or ss formats.

Source Code

package com.beginner.examples;

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

public class FormattingSeconds {

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

		/*
		 * To format seconds in ss format use SimpleDateFormat("ss") Constructor.
		 */
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("ss");
		System.out.println("Seconds in ss format : " + simpleDateFormat2.format(date));
		
	}

}

Output:

Before formatting , date is : Fri Jun 14 14:27:03 CST 2019
Minutes in m format : 3
Minutes in m format : 03

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments