- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the day of week for a particular date in Java
To get the day of week for a particular date in Java, use the Calendar.DAY_OF_WEEK constant.
Let us set a date first.
Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);
Since, we have used the Calendar as well as GregorianCalendar classes, therefore at first, import the following packages.
import java.util.Calendar; import java.util.GregorianCalendar;
Now, we will find the day of week.
int day = one.get(Calendar.DAY_OF_WEEK);
The following is an example
Example
import java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] argv) throws Exception { Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10); int day = one.get(Calendar.DAY_OF_WEEK); System.out.println(day); Calendar two = new GregorianCalendar(2011, Calendar.AUGUST, 9); day = two.get(Calendar.DAY_OF_WEEK); System.out.println(day); Calendar three = new GregorianCalendar(2017, Calendar.OCTOBER, 22); day = three.get(Calendar.DAY_OF_WEEK); System.out.println(day); Calendar four = new GregorianCalendar(2018, Calendar.DECEMBER, 30); day = four.get(Calendar.DAY_OF_WEEK); System.out.println(day); } }
Output
7 3 1 1
Advertisements