create a date for the first of month

Posted on 2006-06-12
Last Modified: 2010-03-31
i want to create a java.util.Date object for the first of the current month !

how ?
Question by:letsbedecent
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 16889299
I used

int month = Calendar.getInstance().get(Calendar.MONTH);
int year = Calendar.getInstance().get(Calendar.YEAR);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = sdf.parse(month + "/01/" + year);

is there a better way of doing this ?
LVL 92

Expert Comment

ID: 16889896
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 1);
Date first = cal.getTime();
LVL 10

Expert Comment

ID: 16891199
it should be
cal.set ( Calendar.MONTH , 0 );  for January.


Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Expert Comment

ID: 16891283
you can also set by cal.set ( Calendar.MONTH, Calendar.JANUARY )
LVL 10

Accepted Solution

mukundha_expert earned 105 total points
ID: 16891794
Sorry ,

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
Date first = cal.getTime();
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 20 total points
ID: 16891875
>>  want to create a java.util.Date object for the first of the current month

Since it is current month, you don't need to set the month using cal.set ( Calendar.MONTH.... ). Just set the Calendar.DATE part as mukundha has shown (and yes, DATE starts with 1 whereas MONTH starts with 0).

Author Comment

ID: 16891884
Thank you !

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 160
jboss wildfly 10.1 10 616
Better way to make a string with template variables in java 3 53
Java ArrayList and if statement 2 55
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question