java date format problem

Discussion in 'Java' started by Amit Kumar, Feb 15, 2006.

  1. Amit Kumar

    Amit Kumar Guest

    How to get current date in format dd-mon-yyyy in java???
    Amit Kumar, Feb 15, 2006
    1. Advertisements

  2. Amit Kumar wrote:
    > How to get current date in format dd-mon-yyyy in java???

    I think google is your friend...

    tom fredriksen, Feb 15, 2006
    1. Advertisements

  3. Amit Kumar schrieb:
    > How to get current date in format dd-mon-yyyy in java???


    Matthias Reining, Feb 15, 2006
  4. I think the easiest approach would be to get the current date by using
    function: sysdate() and then change its format to "dd-mon-yyyy" by
    using the function :SimpleDateFormat("dd-mon-yyyy").

    Biswajit Biswal, Feb 15, 2006
  5. Rhino

    Rhino Guest

    "Amit Kumar" <> wrote in message
    > How to get current date in format dd-mon-yyyy in java???

    I'll get you started....

    I wrote a utilities class to get the current date in YYYY-MM-DD format, e.g.
    2006-02-15, and used the following code to get the parts of the date:

    GregorianCalendar now = new GregorianCalendar();
    int intCurrentYear = now.get(Calendar.YEAR);
    int intCurrentMonth = now.get(Calendar.MONTH) + 1;
    int intCurrentDay = now.get(Calendar.DAY_OF_MONTH);

    (Month numbers in Java are 0-based, i.e. month 0 = January, month 1 =
    February, etc. That's why I'm adding one to the month number.)

    You could write an enum to help convert month numbers to month names if you
    want month names instead of month numbers. Or just write an if:

    String strCurrentMonth = null;
    if (intCurrentMonth = 1) strCurrentMonth = "Jan"
    else if (intCurrentMonth = 2) strCurrentMonth = "Feb"
    else if(intCurrentMonth = 12) strCurrentMonth = "Dec"
    else {
    System.err.println("Invalid month number");

    Then, assuming you want the final date to be a String, you'll need to
    convert these integers to Strings. For example:

    String strCurrentDay = String.valueOf(intCurrentDay);

    Then, concatenate the different Strings together with the hyphen separators
    and you're done.

    (You may want to pad the month and day numbers with leading zeroes in case
    they are less than 10, otherwise, you'll get Strings that look like
    "15-2-2006" instead of "15-02-2006". Then again, you may prefer not to have
    the leading zeroes. That's up to you.)

    Rhino, Feb 15, 2006
  6. Amit Kumar

    Amit Kumar Guest

    I got the date as a string But I want it to be in the date variable how
    to convert this string into date variable ..Please help.
    Amit Kumar, Feb 16, 2006
  7. Rhino

    Rhino Guest

    "Amit Kumar" <> wrote in message
    >I got the date as a string But I want it to be in the date variable how
    > to convert this string into date variable ..Please help.

    I don't understand what you are trying to do.

    The value in a date variable - I assume you mean an instance of a
    java.util.Date class - does not look like dd-mon-yyyy or yyyy-mm-dd or
    anything close to that. If you look at a Date variable in a Java debugger,
    like the one in Eclipse, you'll see that it is basically a long that
    represents the number of milliseconds since Jan 1, 1970.

    I just created the following trivial Java class:

    import java.util.Date;

    public class Dates {

    public static void main(String[] args) {
    new Dates();

    public Dates() {
    Date now = new Date();
    System.out.println("now=" + now);

    When I ran this class in the Eclipse debugger, I got the following after the
    first line in the constructor had executed:

    now=Date (id=11)

    That's all; the full contents of the variable. If you calculate the number
    of milliseconds that have elapsed since Jan 1, 1970, I think you will find
    that it comes very close the to the value in 'fastTime'.

    You can use a Date like this in calculations but it isn't obvious what the
    day, month and year are for this date. If you print the value of the 'now'
    variable, as I did in my code, you get a value like this:

    now=Thu Feb 16 00:24:02 EST 2006

    But I think that is some magic that println() is doing. I'm not aware of any
    way to coax println() to display the value as dd-mon-yyyy. However, you
    _can_ do what I showed you in my original post if you want to see the
    current date in dd-mon-yyyy; it sounds like you have done that.

    You have to understand that a java.util.Date is a real Date variable that
    you can use in a calculation but that it isn't going to be automatically
    displayable in the format you want. You can do something like I showed you
    in my previous reply but that is simply a String that looks the way you want
    it to look; you can't use that String directly in a date calculation. In
    other words, the VALUE of the date is not the same as the DISPLAY
    PRESENTATION of the date. You have to decide what you want.

    By the way, I'm _not_ saying that you have to choose whether you are going
    to use the Date in a calculation or display it, as if you can only do one or
    the other. I'm trying to say that any given variable can only be used in a
    calculation or displayed BUT NOT BOTH AT THE SAME TIME. You _can_ convert
    from one to the other and back again.

    For instance, if you want to take the String that you have generated based
    on my previous suggestion to a Date that can be used in a calculation, the
    following code converts a String that looks like "2006-02-15" to a
    java.util.Date value. You need to modify it to return the format you want -
    dd-mon-yyyy - instead of YYYY-MM-DD. Look at the SimpleDateFormat class to
    see the codes you'll need for the first line. Then if you use the code I
    suggested in my previous post to get the date into a String format, and then
    execute the lines that you base on the following code, you should get what
    you want, you should find the desired code in the utilDate variable.

    java.text.SimpleDateFormat sdf = new

    java.util.Date utilDate = null;
    try {
    utilDate = sdf.parse(date);
    } catch (ParseException p_excp) {
    throw new IllegalArgumentException("Encountered ParseException
    at offset " + p_excp.getErrorOffset() + " while attempting to convert the
    String " + date + " to a java.util.Date. Details: " + p_excp.getMessage());

    But that's probably the long way to get what you want. If you had told me
    that you wanted a Date in the first place, I would have advised something

    The simplest way to get the current date to be a variable that is an
    instance of the java.util.Date class is to simply do what I did earlier in
    this note:

    java.util.Date now = new java.util.Date();

    The variable 'now' is an instance of a java.util.Date and you can use it in
    calculations. Or you can convert it to a String to display it in the format
    you like via the techniques I've described in my previous note.

    I hope I haven't confused you with this; if I have, please say so and I'll
    try to clarify.

    Oh, one more thing. I've tried to be careful to say that I am talking about
    java.util.Date, not just Date. That's because there are _two_ Date classes:
    java.util.Date and java.sql.Date. They're actually closely related but you
    probably want to stick with java.util.Date unless you are trying to write
    dates that are being stored in a database.

    Rhino, Feb 16, 2006
  8. Amit Kumar wrote:
    > I got the date as a string But I want it to be in the date variable how
    > to convert this string into date variable ..Please help.

    Please read the JavaDoc API documentation of SimpleDateFormat. You have
    been pointed to it multiple times.
    Thomas Weidenfeller, Feb 16, 2006
  9. Roedy Green

    Roedy Green Guest

    On 15 Feb 2006 08:30:07 -0800, "Amit Kumar" <>
    wrote, quoted or indirectly quoted someone who said :

    >How to get current date in format dd-mon-yyyy in java???

    Canadian Mind Products, Roedy Green. Java custom programming, consulting and coaching.
    Roedy Green, Feb 17, 2006
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Feb 18, 2004
  2. DC
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Michael Borgwardt
    May 30, 2004
  4. Chu
    Aug 15, 2006
  5. Une bévue

    problem with date and date/format

    Une bévue, Mar 10, 2006, in forum: Ruby
    Une bévue
    Mar 12, 2006

Share This Page