java date format problem

B

Biswajit Biswal

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
 
R

Rhino

Amit Kumar said:
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.)
 
A

Amit Kumar

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

Rhino

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

Thomas Weidenfeller

Amit said:
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.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top