how to get current date?

E

Ekim

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!
 
T

TechBookReport

Ekim said:
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.
 
T

Thomas Weidenfeller

Ekim said:
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
 
K

KD

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
 
C

Chris Smith

Hi Ekim,

Here's some more detail on your second problem.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top