What is the date on Monday in Current week in Java?
Problem Description :
Concept :
The solution of this problem is very simple. We just need to use DAY_OF_WEEK constant and set() method of Calendar class that is present in java.util.* package.
Recommended :
Java Program :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.number; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Date and Time Problems | |
* @level Basic | |
* @problem What is the date on monday in current week? | |
* | |
*/ | |
public class PrintMondayDateofCurrentWeek { | |
public static void main(String[] args) { | |
// Creating Calendar class instance. | |
Calendar calendar = Calendar.getInstance(); | |
String currentDate = getStringFromCalendar(calendar); | |
System.out.println("Current date : " + currentDate); | |
// In Calendar setting day of week = 2(Monday) | |
calendar.set(Calendar.DAY_OF_WEEK, 2); | |
currentDate = getStringFromCalendar(calendar); | |
System.out.println("Date on Monday : " + currentDate); | |
} | |
private static String getStringFromCalendar(Calendar calendar) { | |
// Converting Calendar to Date. | |
Date currentDate = calendar.getTime(); | |
DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); | |
// Converting Date to String. | |
String myCurrentDate = format.format(currentDate); | |
return myCurrentDate; | |
} | |
} |
Output :
Similar Problems :
1) What is the date on Sunday in Current week?
- Replace line 25 with calendar.set(Calendar.DAY_OF_WEEK, 1);
2) What is the date on Tuesday in Current week?
- Replace line 25 with calendar.set(Calendar.DAY_OF_WEEK, 3);
3) What is the date on Wednesday in Current week?
- Replace line 25 with calendar.set(Calendar.DAY_OF_WEEK, 4);
4) What is the date on Thursday in Current week?
- Replace line 25 with calendar.set(Calendar.DAY_OF_WEEK, 5);
5) What is the date on Friday in Current week?
- Replace line 25 with calendar.set(Calendar.DAY_OF_WEEK, 6);
6) What is the date on Saturday in Current week?
- Replace line 25 with calendar.set(Calendar.DAY_OF_WEEK, 7);
References :
https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
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, Date on week day in Java, Solution in Java, Calendar, DateFormat, SimpleDateFormat.
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, Date on week day in Java, Solution in Java, Calendar, DateFormat, SimpleDateFormat.
What is the date on Monday in Current week in Java?
Reviewed by Rohit Agarwal
on
2/01/2017
Rating:

Brunei yurtdışı kargo
ReplyDeleteBrezilya yurtdışı kargo
Botswana yurtdışı kargo
Bosna Hersek yurtdışı kargo
Burundi yurtdışı kargo
2İ2W