Formatting seconds using SimpleDateFormat - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

Formatting seconds using SimpleDateFormat

Demo Code



import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
 
  public static void main(String[] args) {
     Date date = new Date();
   /*  w  w w.  j a va  2  s  .  c  o m*/
     //formatting seconds in s format like 1,2 etc.
     String strDateFormat = "s";
     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
     
     System.out.println("seconds in s format : " + sdf.format(date));
     
     //formatting seconds in ss format like 01, 02 etc.
     strDateFormat = "ss";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("seconds in ss format : " + sdf.format(date));
  }
}

Result


Related Tutorials