Java SimpleDateFormat format second as "1", "01"

Description

Java SimpleDateFormat format second as "1", "01"

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

public class Main {

  public static void main(String[] args) {
    Date date = new Date();

    // formatting seconds in s format like 1,2 etc.
    SimpleDateFormat sdf = new SimpleDateFormat("s");
    System.out.println("seconds in s format : " + sdf.format(date));

    // formatting seconds in ss format like 01, 02 etc.
    sdf = new SimpleDateFormat("ss");
    System.out.println("seconds in ss format : " + sdf.format(date));
  }//from  w w w.  ja  v  a2  s .c o m
}



PreviousNext

Related