Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get current date?

Reply
Thread Tools

how to get current date?

 
 
Ekim
Guest
Posts: n/a
 
      02-13-2004
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!


 
Reply With Quote
 
 
 
 
TechBookReport
Guest
Posts: n/a
 
      02-13-2004

"Ekim" <(E-Mail Removed)> wrote in message
news:c0ic0e$17v0ar$(E-Mail Removed)-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.


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      02-13-2004
Ekim wrote:
> I'm not quite familiar with the Java language, and therefore I have
> some (assumed to be simple) questions.


For simple answers..
http://groups.google.com.au/groups?g...lang.java.help

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-13-2004
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

 
Reply With Quote
 
KD
Guest
Posts: n/a
 
      02-13-2004
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!
>
>

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      02-13-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the current method name within current method Matthew Heidemann Ruby 14 03-22-2011 03:56 AM
printing out the current URL for current ASP page in Perl Jack Perl Misc 3 12-22-2008 02:35 PM
Thread.current.parent and Thread.current.ancestors ara.t.howard@noaa.gov Ruby 3 02-16-2007 03:34 PM
How to update the current web page? (delete current web page + rewrite the page) laryten@yahoo.com Javascript 3 10-12-2006 07:33 PM
How to change the current thread current culture at run time. Manu ASP .Net 1 07-20-2003 03:27 PM



Advertisments