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 problem in linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "date problem in linux" Watch "date problem in linux" New topic

date problem in linux

Rocky Salve

Joined: Apr 05, 2006
Posts: 16

when i run the date command in fedora, i get the current month
displayed. but if i want to display the previous month how is it
possible anyway.

anyone gotta a bulb!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

It might seem a silly question, but how do you define 'date of the previous month'.

It seems to be easy, i.e.: you take the 15th of July, and the previous month it was the 15th of June.
But what do you do on 30th March?
Do you want the last day of February?
Do you want to substract 30 days?

There are different definitions for 'month' in financial sector, and in law.

I found this 30-days solution (but didn't test it for critical dates):

Perhaps a java solution using GregorianCalendar will be good.
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
date can do some tricky stuff...

You could do
date -d yesterday
date -d now-30days - which today (apr 11, gives me mar12).

So I would imagine that you could do some kind of lookup based on how many days there are in the month. and then do now-xxxdays to get last month.

I have no java certifications. This makes me a bad programmer. Ignore my post.
I agree. Here's the link:
subject: date problem in linux
It's not a secret anymore!