BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to get current day, month, year, day of week/month/year in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will see how to get current date, day, month, year, day of week, day of month and day of year in java.

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

class Example {

   public static void main(String args[]) {
      Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
      //getTime() returns the current date in default time zone
      Date date = calendar.getTime();
      int day = calendar.get(Calendar.DATE);
      //Note: +1 the month for current month
      int month = calendar.get(Calendar.MONTH) + 1;
      int year = calendar.get(Calendar.YEAR);
      int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
      int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
      int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);

      System.out.println("Current Date is: " + date);
      System.out.println("Current Day is:: " + day);
      System.out.println("Current Month is:: " + month);
      System.out.println("Current Year is: " + year);
      System.out.println("Current Day of Week is: " + dayOfWeek);
      System.out.println("Current Day of Month is: " + dayOfMonth);
      System.out.println("Current Day of Year is: " + dayOfYear);

   }
}

Output:

Current Date is: Wed Jan 08 20:30:26 IST 2014
Current Day is:: 8
Current Month is:: 1
Current Year is: 2014
Current Day of Week is: 4
Current Day of Month is: 8
Current Day of Year is: 8

In the above example we have fetched the data of local timezone (note: we have used TimeZone.getDefault() in the first statement of the program. However it is also possible to get these details for another TimeZone.

Here is how to do it:

Calendar cal = Calendar.getInstance(TimeZone.getDefault());
//change the timezone: Provide the TimeZone Id as parameter
cal.setTimeZone(TimeZone.getTimeZone("Europe/Athens"));
//get the details in new time zone
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(cal.get(Calendar.WEEK_OF_MONTH));
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
...

Here Europe/Athens is the Id of a TimeZone. In order to know the all the Ids, run the following code. It would give you all existing Ids which you can use in your program:

for (String string : TimeZone.getAvailableIDs()) {
      System.out.println(string);
}

Output would be like:

Etc/GMT+12
Etc/GMT+11
Pacific/Midway
Pacific/Niue
Pacific/Pago_Pago
Pacific/Samoa
US/Samoa
America/Adak
America/Atka
Etc/GMT+10
HST
....
...

References:

  • http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
  • http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html

Top Related Articles:

  1. Date Formatting In Java With Time Zone
  2. Java 8 – Calculate days between two dates
  3. How to Parse Date in Desired format – Java Date
  4. Java Date and Time
  5. Java Year class explained with examples

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap