how to get current date?

Discussion in 'Java' started by Ekim, Feb 13, 2004.

  1. Ekim

    Ekim Guest

    I'm not quite familiar with the Java language, and therefore I have some
    (assumed to be simple) questions.

    How can I get the current systemdate in the format "dd-mm-yyyy".
    I've already looked up in the API, and figured out that it can be done with
    the "Date"-Class. Anyway I was not able to manage it.

    My 2nd problem concerns rounding a double-number. I want to round a number
    like "40.745043" to "40.75", so that it is rounded to 2 digits after the
    decimal point.

    I hope someone can help me answer those questions,

    thx in advance,
    ekim!
     
    Ekim, Feb 13, 2004
    #1
    1. Advertising

  2. "Ekim" <> wrote in message
    news:c0ic0e$17v0ar$-berlin.de...
    > I'm not quite familiar with the Java language, and therefore I have some
    > (assumed to be simple) questions.
    >
    > How can I get the current systemdate in the format "dd-mm-yyyy".
    > I've already looked up in the API, and figured out that it can be done

    with
    > the "Date"-Class. Anyway I was not able to manage it.
    >
    > My 2nd problem concerns rounding a double-number. I want to round a number
    > like "40.745043" to "40.75", so that it is rounded to 2 digits after the
    > decimal point.
    >
    > I hope someone can help me answer those questions,
    >
    > thx in advance,
    > ekim!
    >
    >


    If you only want to round the number for display purpses then look at the
    DecimalFormat class.
     
    TechBookReport, Feb 13, 2004
    #2
    1. Advertising

  3. Andrew Thompson, Feb 13, 2004
    #3
  4. Ekim wrote:
    > I'm not quite familiar with the Java language, and therefore I have some
    > (assumed to be simple) questions.


    Consider posting your beginner's questions to comp.lang.java.help.

    > How can I get the current systemdate in the format "dd-mm-yyyy".
    > I've already looked up in the API, and figured out that it can be done with
    > the "Date"-Class. Anyway I was not able to manage it.


    SimpleDateFormat

    > My 2nd problem concerns rounding a double-number. I want to round a number
    > like "40.745043" to "40.75", so that it is rounded to 2 digits after the
    > decimal point.


    DecimalFormat.

    Both in the java.text package.

    /Thomas
     
    Thomas Weidenfeller, Feb 13, 2004
    #4
  5. Ekim

    KD Guest

    Hi

    Question 1

    import java.util.*

    Date date = new Date();
    // Keep the MM upper case
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    String formattedDate = df.format(date);

    Question 2

    If I remember correctly.

    BigDecimal num = new BigDecimal("40.745043");
    // You have to set this immediately. BigDecimal objects can never be
    // changed - they are immutable
    String myNewNum = num.setScale(2, BigDecimal.ROUND_UP).toString();

    Good Luck
    -Karim

    Ekim wrote:
    > I'm not quite familiar with the Java language, and therefore I have some
    > (assumed to be simple) questions.
    >
    > How can I get the current systemdate in the format "dd-mm-yyyy".
    > I've already looked up in the API, and figured out that it can be done with
    > the "Date"-Class. Anyway I was not able to manage it.
    >
    > My 2nd problem concerns rounding a double-number. I want to round a number
    > like "40.745043" to "40.75", so that it is rounded to 2 digits after the
    > decimal point.
    >
    > I hope someone can help me answer those questions,
    >
    > thx in advance,
    > ekim!
    >
    >
     
    KD, Feb 13, 2004
    #5
  6. Ekim

    Chris Smith Guest

    Hi Ekim,

    Here's some more detail on your second problem.

    Ekim wrote:
    > My 2nd problem concerns rounding a double-number. I want to round a number
    > like "40.745043" to "40.75", so that it is rounded to 2 digits after the
    > decimal point.


    The 'double' data type contains binary floating-point numbers rather
    than decimal floating-point numbers. While 40.75 does happen to be
    directly representable as a binary floating-point value (.01), the
    general case of rounding a binary floating-point number to two decimal
    digits is not even possible, since the floating-point number often won't
    be able to exactly represent that value.

    Depending on your application, though, there are solutions. For
    example:

    1. If this is just for display purposes, then you can use DecimalFormat
    to get a rounded String. That works because the target type is String,
    not double.

    2. For intermediate results that need to be rounded (as required, for
    example, in a number of financial calculations), a better choice is
    often the BigDecimal class or a fixed-point representation using an int
    (or long) and a constant factor -- for example, the integer 12345 might
    be used to represent $123.45.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 13, 2004
    #6
    1. Advertising

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. Matt
    Replies:
    1
    Views:
    610
    Matthew Speed
    Nov 8, 2003
  2. James P.
    Replies:
    7
    Views:
    12,533
    phamtasmic
    Jul 13, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
  4. moonhk
    Replies:
    5
    Views:
    550
    moonhk
    Oct 11, 2006
  5. Michael Haas

    compare date field to current date

    Michael Haas, Feb 15, 2004, in forum: ASP General
    Replies:
    4
    Views:
    289
    Mike Haas
    Feb 15, 2004
Loading...

Share This Page