Displays the name of the day, date, month, and year in java

By . Category: Java Tags:

Irnanto.comDisplays the name of the day, date, month, and year in java using standard functions and formats that already exist in java. There are many ways to come up with the name format, date, month and year in java applications. Below are some examples of date formats.

  • 01-Dec-2013
  • Sunday, 1 December 2013
  • 1 December 2013, 00:00:01
  • 01-December-2013
  • 01/12/2013
  • etc

We can make the format of the date, month, year, and name of the day in accordance with our wishes. You just need to know the use of formats such as D, M, y, H, S, and E. Are you still confused with the formats ???. OK I’ll explain further below.

  • d is the initialization to interpret the day and used to call its date. If the format in d just write it, then that will be displayed one digit in the date, and if you want to display a 2-digit date then use dd.
  • M is to define month initialization and is used to invoke the name of the month or the numbers on the order of months. If the format is written MM only then will display the format of two-digit month. If the format is written MMM will display the name of the month with a three-letter format (eg Jan), and to display the full name of the month (eg, January) then use the format MMMM.
  • y is the initialization to interpret the year or years and used to call the year. Format is often used, there are two kinds, namely the first one featuring a four-digit character year (eg 2013) by using the format yyyyy and the second yy format to display behind the two characters (eg 13).
  • H is the initialization to interpret Hour or hours. Format that is often used is HH.
  • m is the initialization to interpret minute or minutes. Format that is often used is mm.
  • s is the initialization to define a second or seconds. Format that is often used is to call the second ss.
  • E is the initialization to interpret or call today. Use EEEE format to be able to call the name of the day as a whole.

Examples of the source code showing the name of the day, date, month, and year in java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class BuatTanggal {
  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

    SimpleDateFormat contoh1 = new SimpleDateFormat(“EE, dd/MM/yyyy HH:mm:ss”);
    SimpleDateFormat contoh2 = new SimpleDateFormat(“dd-MM-yyyy”);
    SimpleDateFormat contoh3 = new SimpleDateFormat(“d-MMM-yyyy”);
    SimpleDateFormat contoh4 = new SimpleDateFormat(“EEEE, d MMMM yyyy”);
    SimpleDateFormat contoh5 = new SimpleDateFormat(“HH:mm:ss”);

    String Tanggal1 = contoh1.format(cal.getTime());
    String Tanggal2 = contoh2.format(cal.getTime());
    String Tanggal3 = contoh3.format(cal.getTime());
    String Tanggal4 = contoh4.format(cal.getTime());
    String Tanggal5 = contoh5.format(cal.getTime());

    System.out.println(Tanggal1);
    System.out.println(Tanggal2);
    System.out.println(Tanggal3);
    System.out.println(Tanggal4);
    System.out.println(Tanggal5);
  }
}

Its results of its output  : Sun, 08/12/2013 18:36:54 08-12-2013 8-Dec-2013 Sunday, 8 December 2013 18:36:54

OK, so here article and source code showing the name of the day, date, month, and year in java. Look forward to other interesting articles on irnanto.com.

Ads