Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get the Day of the Week from Today's Date in Java
To get the day of the week, use the Calendar.DAY_OF_WEEK.
Firstly, let us get the current date.
java.util.Date utilDate = new java.util.Date(); java.sql.Date dt = new java.sql.Date(utilDate.getTime());
Now, using GregorianCalendar, set the time.
java.util.GregorianCalendar cal = new java.util.GregorianCalendar(); cal.setTime(dt);
The last step would display the day of the week as shown in the following example.
Example
import java.text.ParseException;
public class Demo {
public static void main(String[] args) throws ParseException {
java.util.Date utilDate = new java.util.Date();
java.sql.Date dt = new java.sql.Date(utilDate.getTime());
System.out.println("Today's date: "+dt);
java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.setTime(dt);
// Getting the day of the week
System.out.println("Day of the week: "+cal.get(java.util.Calendar.DAY_OF_WEEK));
}
}
Output
Today's date: 2018-11-19 Day of the week: 2
Advertisements
