Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java date format problem

Reply
Thread Tools

java date format problem

 
 
Amit Kumar
Guest
Posts: n/a
 
      02-15-2006
How to get current date in format dd-mon-yyyy in java???

 
Reply With Quote
 
 
 
 
tom fredriksen
Guest
Posts: n/a
 
      02-15-2006
Amit Kumar wrote:
> How to get current date in format dd-mon-yyyy in java???
>


I think google is your friend...

/tom
 
Reply With Quote
 
 
 
 
Matthias Reining
Guest
Posts: n/a
 
      02-15-2006
Amit Kumar schrieb:
> How to get current date in format dd-mon-yyyy in java???
>

java.text.SimpleDateFormat

matthias
 
Reply With Quote
 
Biswajit Biswal
Guest
Posts: n/a
 
      02-15-2006
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

 
Reply With Quote
 
Rhino
Guest
Posts: n/a
 
      02-15-2006

"Amit Kumar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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");
System.exit(16);
}


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


 
Reply With Quote
 
Amit Kumar
Guest
Posts: n/a
 
      02-16-2006
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.

 
Reply With Quote
 
Rhino
Guest
Posts: n/a
 
      02-16-2006

"Amit Kumar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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)
|
|--cdate=null
|--fastTime=1140067442401

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.text.SimpleDateFormat("yyyy-MM-dd");

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
different.

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




 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-16-2006
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-17-2006
On 15 Feb 2006 08:30:07 -0800, "Amit Kumar" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

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


see http://mindprod.com/jgloss/today.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
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
Converting java.util.Date to java.sql.Date mail4sushovan@gmail.com Java 5 05-04-2011 02:59 PM
problem with date and date/format Une bévue Ruby 3 03-12-2006 06:00 AM
[Newbie UK Date problem] Ways atround this UK date format localisation/SQLselection issue? DC ASP .Net 5 07-01-2005 09:35 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments