get first day of month

Have a Date object

empDate

need to get the first day of the month for the empDate

How to get this?

Thanks.

[137 byte] By [Rxyza] at [2007-11-27 10:44:36]
# 1

Calendar cal = new GregorianCalendar();

cal.setTime(empDate);

cal.set(Calendar.DATE /* or DAY_OF_MONTH */ , 1);

Date firstOfMonth = cal.getTime();

jverda at 2007-7-28 20:07:51 > top of Java-index,Java Essentials,New To Java...
# 2

cal.set(Calendar.DATE /* or DAY_OF_MONTH */ , 1);

not sure what should be put here?

can you give an example?

if 03/17/2007 is the date

Date empDate = use SimpleDateFormat...

Calendar cal = new GregorianCalendar();

cal.setTime(empDate);

cal.set(03 , 1); ?

Date firstOfMonth = cal.getTime();

Should it be something like above?

Thanks.

Rxyza at 2007-7-28 20:07:51 > top of Java-index,Java Essentials,New To Java...
# 3

> cal.set(Calendar.DATE /* or DAY_OF_MONTH */ , 1);

>

> not sure what should be put here?

>

> can you give an example?

>

> if 03/17/2007 is the date

> Date empDate = use SimpleDateFormat...

>

>

> Calendar cal = new GregorianCalendar();

> cal.setTime(empDate);

> cal.set(03 , 1); ?

> Date firstOfMonth = cal.getTime();

>

> Should it be something like above?

>

> Thanks.

Calendar cal = new GregorianCalendar();

cal.setTime(empDate);

cal.set(Calendar.DAY_OF_MONTH , 1); // jverd told you exactly what to put there

Date firstOfMonth = cal.getTime();

SomeoneElsea at 2007-7-28 20:07:51 > top of Java-index,Java Essentials,New To Java...
# 4

> cal.set(Calendar.DATE /* or DAY_OF_MONTH */ , 1);

>

> not sure what should be put here?

EIther DATE or DAY_OF_MONTH. They both mean the same thing. You pick which one is clearer to you.

I assumed you'd read the docs for the classes, methods, and fields I provided.

> can you give an example?

That whole post WAS an example. Hell, it was a complete solution.

> if 03/17/2007 is the date

> Date empDate = use SimpleDateFormat...

Oh, so you don't have a Date yet, just a String? I thought you were starting with a Date.

http://www.javaworld.com/jw-12-2000/jw-1229-dates.html

http://www.javaalmanac.com/egs/java.text/ParseDate.html

> cal.set(03 , 1); ?

Where the hell did you get that from? Who said anything about "03"?

> Should it be something like above?

What do you mean? I showed you exactly how to do it. Take that code. Play with it. Read the docs for the relevant methods, etc. Understand what it does. If you don't understand some part, ask a specific question. Adapt it for your needs. If you get stuck, ask a specific question.

jverda at 2007-7-28 20:07:51 > top of Java-index,Java Essentials,New To Java...