How to get all the information about current date in Java?



Problem Description :


Write a program in that prints following information about current date in Java.

  • Day of month
  • Month number
  • Month name
  • Year
  • Day number of week
  • Day name
  • Week of month
  • Week of year
  • Hour in 12-hour format
  • Hour in 24-hour format
  • Minutes
  • Seconds
  • Milliseconds 

Concept :


We are using following method of Calendar class that is present in java.util.* package.

public int get(int field) -> In this method we are passing following Calendar class constants for getting required information.

  • static int DAY_OF_MONTH -> Provides Day of month.
  • static int MONTH -> Provides Month number, for getting month name we are using switch case.
  • static int YEAR -> Provodes Year.
  • static int DAY_OF_WEEK -> Provides Day number of week, for getting day name we are using switch case.
  • static int WEEK_OF_MONTH -> Provides Week number of month.
  • static int WEEK_OF_YEAR -> Provides Week number of year.
  • static int HOUR -> Provides Hour in 12-hour format.
  • static int HOUR_OF_DAY -> Provides Hour in 24-hour format.
  • static int MINUTE -> Provides Minutes.
  • static int SECOND -> Provides Seconds.
  • static int MILLISECOND -> Provides Milliseconds.


Recommended :





Java Program :


package com.javamultiplex.datetime;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author Rohit Agarwal
 * @category Date and Time
 * @problem How to get all information about current date?
 * 
 */
public class DateInformation {

 public static void main(String[] args) {

  // Creating Calendar class instance.
  Calendar calendar = Calendar.getInstance();
  // Converting Calendar to Date.
  Date date = calendar.getTime();
  System.out.println("Current date : " + date);
  System.out.println("\n--Date information--");
  int dayofMonth = calendar.get(Calendar.DAY_OF_MONTH);
  System.out.println("Day of month : " + dayofMonth);
  int month = calendar.get(Calendar.MONTH); // 0 - January, 11 - December
  System.out.println("Month number : " + (month + 1));
  String monthName = getMonthName(month);
  System.out.println("Month Name: " + monthName);
  int year = calendar.get(Calendar.YEAR);
  System.out.println("Year : " + year);
  int dayofWeek = calendar.get(Calendar.DAY_OF_WEEK);
  System.out.println("Day of week : " + dayofWeek);
  String dayName = getDayName(dayofWeek);
  System.out.println("Day name : " + dayName);
  int weekofMonth = calendar.get(Calendar.WEEK_OF_MONTH);
  System.out.println("Week of month :" + weekofMonth);
  int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
  System.out.println("Week of year : " + weekofYear);
  int hour = calendar.get(Calendar.HOUR);
  System.out.println("Hour in 12-hour format : " + hour);
  int hourofDay = calendar.get(Calendar.HOUR_OF_DAY);
  System.out.println("Hour in 24-hour format : " + hourofDay);
  int minute = calendar.get(Calendar.MINUTE);
  System.out.println("Minutes : " + minute);
  int second = calendar.get(Calendar.SECOND);
  System.out.println("Seconds : " + second);
  int millisecond = calendar.get(Calendar.MILLISECOND);
  System.out.println("Milliseconds : " + millisecond);
 }

 private static String getDayName(int dayofWeek) {

  String dayName = null;
  switch (dayofWeek) {
  case 1:
   dayName = "Sunday";
   break;
  case 2:
   dayName = "Monday";
   break;
  case 3:
   dayName = "Tuesday";
   break;
  case 4:
   dayName = "Wednesday";
   break;
  case 5:
   dayName = "Thursday";
   break;
  case 6:
   dayName = "Friday";
   break;
  case 7:
   dayName = "Saturday";
   break;
  }
  return dayName;
 }

 private static String getMonthName(int month) {

  String monthName = null;
  switch (month) {
  case 0:
   monthName = "January";
   break;
  case 1:
   monthName = "February";
   break;
  case 2:
   monthName = "March";
   break;
  case 3:
   monthName = "April";
   break;
  case 4:
   monthName = "May";
   break;
  case 5:
   monthName = "June";
   break;
  case 6:
   monthName = "July";
   break;
  case 7:
   monthName = "August";
   break;
  case 8:
   monthName = "September";
   break;
  case 9:
   monthName = "October";
   break;
  case 10:
   monthName = "November";
   break;
  case 11:
   monthName = "December";
   break;
  }

  return monthName;
 }

}


Output :


Output of Java program that prints every information about current date.
Output - How to print every information about current date in Java?


Similar Problem :


How to get all the information about any given date in Java?

Solution - This is very simple first of all Convert given Date to Calendar and rest is same as above.


References :



Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Date and Time problems, Every information about date in Java, Solution in Java, Calendar, switch statement.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to get all the information about current date in Java? How to get all the information about current date in Java? Reviewed by Rohit Agarwal on 12/08/2016 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.