Author Topic: Java and dates  (Read 3097 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14309
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Java and dates
« on: March 17, 2011, 09:42:32 AM »
This might be a stupid question, but how do I take a date in pretty much any format (because our clients specify date formats at the application and question level), and pull out the year/month/day?

Why isn't there a php.net type manual for Java?  ARGH!

Dumah

  • Jackass IV
  • Posts: 960
  • Karma: +21/-6

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: Java and dates
« Reply #2 on: March 17, 2011, 09:56:53 AM »
Apache Commons DateUtils has several methods that you might find useful.
http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/time/DateUtils.html

I've never used it, but I've seen JodaTime recommended in Stack Overflow threads as well
http://joda-time.sourceforge.net

PJYelton

  • Power of Voodoo
  • Jackass In Charge
  • Posts: 1597
  • Karma: +56/-12
    • TheRecursiveFractal
Re: Java and dates
« Reply #3 on: March 17, 2011, 12:46:35 PM »
We use SimpleDateFormat also and it works well for us.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14309
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Java and dates
« Reply #4 on: March 17, 2011, 01:20:24 PM »
OK, so I have the following and it's working to get the date in the right format:

Code: [Select]
String pattern = cell.getFormat();
System.out.println(pattern);
Date activity = new Date();
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
activity = df.parse(cell.getValue());
        System.out.println(df.format(activity));
} catch (ParseException e) {
System.out.println(e.getMessage());
}

Quote
MM/dd/yyyy
03/01/2011
MM/dd/yyyy
03/02/2011
MM/dd/yyyy
03/03/2011
MM/dd/yyyy
03/04/2011
MM/dd/yyyy
03/05/2011
MM/dd/yyyy
03/06/2011
MM/dd/yyyy
03/07/2011
MM/dd/yyyy
03/09/2011

But now I need to grab the specific portions of the date.  Do I need to create new patterns for each portion or is there an easier way to grab the day, month, and year and put them into separate variables?

Mike

  • Jackass In Charge
  • Posts: 11256
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14309
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Java and dates
« Reply #6 on: March 17, 2011, 01:31:29 PM »
Is that really what I need though?  That seems like it's only for dealing with the existing calendar.  I have a date object that I want to parse.  I don't see how I can pass a date variable into that.  Or am I blind?

Mike

  • Jackass In Charge
  • Posts: 11256
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Java and dates
« Reply #7 on: March 17, 2011, 01:43:23 PM »

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: Java and dates
« Reply #8 on: March 17, 2011, 01:54:31 PM »
obes...

calendar creation is relatively expensive in java.  so at the very least avoid local creation especially in loops...

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14309
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Java and dates
« Reply #9 on: March 17, 2011, 01:59:27 PM »
I'm only creating it once.

hans

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3523
  • Karma: +46/-18
Re: Java and dates
« Reply #10 on: March 17, 2011, 03:21:46 PM »
We use JodaTime where I work now, and it's better than trying to work with the Java Calendar object, which is pretty much required if you don't want to use deprecated methods.

This signature intentionally left blank.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14309
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Java and dates
« Reply #11 on: March 17, 2011, 03:33:19 PM »
OK, got it working.  Thanks for the help.