August 19 2016

Java calendar get first day of week

Java calendar get first day of week

Set the Calendar object to be in the week for which you need the first day and set the calendar ‘DAY_OF_WEEK’ to ‘1’. Calendar.get(Calendar.DAY_OF_WEEK) returns int values from 1 to 7(1-Sunday and 7-Saturday).The first day of the week is Sunday.



package com.answersz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class JavaCalendarFirstDayOfWeek {

public static void main(String[] args) throws ParseException {

// Java calendar get first day of week

String date = "17-08-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date dt = sdf.parse(date);

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.DAY_OF_WEEK, 1);

Date firstDayOfWeek = calendar.getTime();

System.out.println("Date : " + sdf.format(dt));

System.out.println("First Day of the Week Date : "
+ sdf.format(firstDayOfWeek));

System.out.println("First day of the week : "
+ calendar.get(Calendar.DAY_OF_WEEK));




Date : 17-08-2016
New Date : 14-08-2016
First day of the week : 1

Copyright © 2014. All rights reserved.

Posted August 19, 2016 by answersz in category Core Java

Leave a Comment

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