What is the date on Monday in Current week in Java?



Problem Description :


Write a program in Java that finds Monday's date of current week?

Java program that finds a date on Monday in current week.
Date on Monday in current week?

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 :


package com.javamultiplex.datetime;

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 :


Output of Java program that finds date on Monday in current week.
Output - What is the date on Monday in current week in Java?

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.

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 |

What is the date on Monday in Current week in Java? What is the date on Monday in Current week in Java? Reviewed by Rohit Agarwal on 2/01/2017 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.