SimpleDateFormat to format year

In this chapter you will learn:

  1. Formatting year using SimpleDateFormat

Formatting year using SimpleDateFormat

yy shows year value in two-digit form, while yyyy makes SimpleDateFormat display the year value in four-digit form.

import java.text.SimpleDateFormat;
import java.util.Date;
//from   j a va 2s.  c  o m
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    // formatting year in yy format like 07, 08 etc

    SimpleDateFormat sdf = new SimpleDateFormat("yy");
    System.out.println("Current year in yy format : " + sdf.format(date));
    // formatting year in yyyy format like 2007, 2008 etc.

    sdf = new SimpleDateFormat("yyyy");
    System.out.println("Current year in yyyy format : " + sdf.format(date));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to combine formatting flags
  2. Add slash to separate fields
Home » Java Tutorial » Date, Time, Calendar, TimeZone

Date

    Date class
    Date comparison
    Converting date value
    DateFormat class
    Format time
    DateFormat and Locale
    Convert String to Date

Calendar

    Calendar class
    Calendar date time getter
    Calendar set hour,minute,second
    Calendar calculation
    Calendar comparison
    Calendar properties
    GregorianCalendar

TimeZone

    TimeZone
    TimeZone ID and display name
    TimeZone daylight saving

DateFormat

    DateFormat class
    Format time
    DateFormat and Locale
    Convert String to Date
    SimpleDateFormat
    Parse date string into Date object
    SimpleDateFormat day format
    SimpleDateFormat Hour Time format
    SimpleDateFormat format minute
    SimpleDateFormat to format month
    SimpleDateFormat to format second value
    SimpleDateFormat to format TimeZone
    SimpleDateFormat to format year
    SimpleDateFormat to combine formatting flags