August 17 2016

Java first day of month

Java first day of month

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


package com.answersz;

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

public class JavaFirstDayOfMonth {

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

// Java first day of month

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

Calendar cal = Calendar.getInstance();
 cal.set(Calendar.DAY_OF_MONTH, 1);

Date firstDate = cal.getTime();

System.out.println("First date of the month : "
 + sdf.format(firstDate));

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




First date of the month : 01-08-2016
First day of the month : 2

Copyright © 2014. All rights reserved.

Posted August 17, 2016 by answersz in category Core Java

Leave a Comment

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