File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes date comman Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "date comman" Watch "date comman" New topic

date comman

raj esh

Joined: Apr 03, 2010
Posts: 10

i just want to write a scipt,which should run on 5st day of everymonth(i will put this in cron).

my requiement is want to get the previous month from current date.
suppose today is 5th apr ,then i need to get 1mar to 1apr data records.
suppose today is 6th mar, then i need to get 1feb to 1 mar.

how can i get the previous month by using the date command in unix.
i dont know how to get the previous months from current . please help somebody.

Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

I would write the script in a language that makes it easy, say Perl. Perl is great for efforts like this.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844


I realize we're talking about Unix here, but please proof-read your posts.

Also, please check your private messages for an important administrative matter.

[Asking smart questions] [About Bear] [Books by Bear]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If you look at the man page for the Unix date command you'll find all of the ouput sequences.
Once you filter out everything but the month, you can use the expr command to subtract 1

Prints: 3 in April

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Bear Bibeault wrote:

I realize we're talking about Unix here,...

Good thing he wasn't looking to find the passwd for a given usr!

Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
You want to keep in mind if the current Month is January(1) then the previous month would be December(12).

You will need to take that into consideration when you develop your algorithm.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Well, let's do a short travel to the bottom of unix-date (gnu-date?) and back, avoiding perl, 'cause, unreadable code can be done without perl, can't it?

Lesson 1: Format the date with +..., + means 'here comes format' %Y and so on might be looked up with date --help immediately:

That was quiete simple, wasn't it? Visit us tomorrow for the next lesson!

Ok. Lesson 2:
Don't be fixed to today, let's jump directly one month back:

You see us repeating lesson 1 with the format, but -d means 'take a date I provide', and then you put in your date, or you do some arithmetic on the current date. -1month is pretty self explantory, but the missing blank is hard to remember.

Therefore we take a little brake and see us tomorrow.

Ok. Here we go. Lesson 3:
We take a small excursus to string-manipulation in bash.
The output of cmd CMD can be put to a variable cmd with cmd=$(CMD)

first is 8 characters starting at index 0 and we put 01 to the end. We could have used:

but we need lastm though.

Now that was a hard lesson - let's have a brake!

Lesson 4 (allways the hardest one):
Not really that hard, wasn't it? Take first as a date, add 1 month, add 1 negative day, fomat it all the way. Cut off 2010-03- (8 characters) in a second step.

Okay - too much effort. We could use this instead:

This was the only useful thing in all those commands, because it will handle 31 or 30 days, as well as leap years and exceptions from leap years and so on. Ok - Month 01-1=12 was useful before.

Now we got what we need, and put it all together, tomorrow...

Lesson 5: Earning the fruit:

Wow. Now put everything in one line, and you'll got perl.

Some goodies for the weekend:

For more details see:
Our solution was, put to one block of code:
I agree. Here's the link:
subject: date comman
It's not a secret anymore!