tags:

views:

66

answers:

3

Hi All

I've a code to get year, month and day for one of my application.

    package com.cera.hyperionUtils;
import java.util.*;

public class HypDate {

 public static int curdate(int field)
 {
  //1. Specify integer 1 for YEAR, 2 for MONTH, 5 DAY_OF_MONTH
  Calendar c = new GregorianCalendar();
  c.setLenient(true); //Allow overflow

  //2. Extract and Return result
   if (field == 2) {
    field = c.get(Calendar.MONTH) + 1;
  }   
  return c.get(field);
 }

 public static void main(String[] args)
 {
 System.out.println(HypDate.curdate(2));

 }
} 

But when i pass 2 it is giving 0 year and day prints correctly.....Also i was trying to make month as double digit. (like 01 for 1)

Can someone please help me....? (I''m very new to java coding)

+3  A: 
   if (field == 2) {
    field = c.get(Calendar.MONTH) + 1;
  }   
  return c.get(field);

You retrieve the correct month as an index and then use that index to retrieve another field that will be unknown and related in how the constants are saved. Just return the value before, without using a second get.

Maybe you meant

   if (field == 2) {
    field = Calendar.MONTH;
  }   
  return c.get(field) + 1;

but I don't get why you are redefining that constants instead that use the one already provided..

Jack
+1  A: 

The problem comes from the fact that when you are getting the month information, you call c.get() twice, which you don't want to do. Instead, you should directly return after you get the first value

  //1. Specify integer 1 for YEAR, 2 for MONTH, 5 DAY_OF_MONTH
  Calendar c = new GregorianCalendar();
  c.setLenient(true); //Allow overflow

  //2. Extract and Return result
   if (field == Calendar.MONTH) {
    return c.get(field) + 1;  //because Java months are 0-based
  } else {  
    return c.get(field);
 }
J. O'Hara
PS If you are trying to do Date formatting, you should look at the SimpleDateFormat class. I get the feeling you are trying to reinvent the wheel here
J. O'Hara
Thank you very much J. O'hara..... That worked..... :)
Celvin
+1  A: 

Rather than returning these one by one, you may just want to use a SimpleDateFormat to format it.

Say I want a date as year-month-day:

// Necessary imports
import java.text.DateFormat;
import java.text.SimpleDateFormat;

// Declare class and stuff before this

public static String getFormattedDate() {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    return df.format(new Date());
}

public static void main(String[] args) {
    System.out.println(getFormattedDate());
}

Outputs 2010-10-29

Edit:

Since you just want the month, you can do this:

public static String getFormattedMonth() {
    DateFormat df = new SimpleDateFormat("MM");

    return df.format(new Date());
}
R. Bemrose
@R. Bemrose I was gonna say the same thing. I would only add that if you still need the seperated, you could follow the same technique and have a getMonth(), getDay() and getYear() method and just change the SimpleDateFormat. Although he was probably trying to build a String like that does in the end anyway.
jschoen
Forgot to mention, `yyyy` forces a four-digit year, `MM` a two digit month, and `dd` a two-digit year.
R. Bemrose
@jschoen: Yeah, I figured he was trying to build up a format as well, which is why I looked up the Syntax for SimpleDateFormat... it's been a while since I had to write one. In fact, the first version of this still used `Calendar`, until I remembered that `new Date()` would do the exact same thing. It also printed minutes instead of months... whoops.
R. Bemrose
The reason why i didnt try SimpleDateFormat was, my application accepts only integers....
Celvin
@Celvin: You can still use SimpleDateFormat, just set the pattern to `"MM"`.
R. Bemrose
when i used the SimpleDateFormat, after compiling and register the package....i get an error saying [invalid type: string]. But the aboe code works....only issue i have is about the month.... if it is less than 10 then it is showing only 1 digit....
Celvin