Formatting year using SimpleDateFormat - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

Formatting year 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();

     //formatting year in yy format like 07, 08 etc
     String strDateFormat = "yy";
     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
     /*w  ww  .  ja  v  a  2 s .  c o  m*/
     System.out.println("Current year in yy format : " + sdf.format(date));
     
     //formatting year in yyyy format like 2007, 2008 etc.
     strDateFormat = "yyyy";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current year in yyyy format : " + sdf.format(date));
  }
}

Result


Related Tutorials