Java Forum / General / March 2006
String to Date
Daniele - 22 Mar 2006 22:49 GMT Let's say that I have a string with this date format "DD/MM/YYYY", could you please write me the code to convert it to date without the TIME.
I tried, but It always converts the String to Data with the time at the end, could you please write me the code to do this ?
Thanks
Luc The Perverse - 22 Mar 2006 22:55 GMT > Let's say that I have a string with this date format "DD/MM/YYYY", could > you please write me the code to convert it to date without the TIME. > > I tried, but It always converts the String to Data with the time at the > end, could you please write me the code to do this ? 1. No one here is going to write code for you. We will answer questions and help you, but not do it for you. 2. Your question doesn't make sense. The string you provided doesn't include any reference to time.
Show us your code, tell us your desired input and output and maybe someone will help you.
-- LTP
:) Oliver Wong - 22 Mar 2006 22:55 GMT > Let's say that I have a string with this date format "DD/MM/YYYY", could > you please write me the code to convert it to date without the TIME. > > I tried, but It always converts the String to Data with the time at the > end, could you please write me the code to do this ? Why don't you post the code you tried? It's not clear to me what you mean by "It always converts the String to Data with the time at the end".
Did you look into the DateFormat.parse() method?
- Oliver
Daniele - 22 Mar 2006 23:08 GMT Well, I didn't think it was that big a deal, they must be 4 lines of code. Anyways it doens't make sense to me too, the String is '22/03/2006' and I don't understand why it the conversion gives me also the time. I'm using the DateFormat.parse() method.
I don't have the code here with me , and since I'm not an expert I don't remember it quite well so.
>> Let's say that I have a string with this date format "DD/MM/YYYY", could >> you please write me the code to convert it to date without the TIME. [quoted text clipped - 8 lines] > > - Oliver Fred Kleinschmidt - 22 Mar 2006 23:34 GMT > Well, I didn't think it was that big a deal, they must be 4 lines of code. > Anyways it doens't make sense to me too, the String is '22/03/2006' and I [quoted text clipped - 16 lines] >> >> - Oliver Use a DateFormatter
 Signature Fred L. Kleinschmidt Boeing Associate Technical Fellow Technical Architect, Software Reuse Project
Oliver Wong - 22 Mar 2006 23:52 GMT [post re-ordered]
>>> Let's say that I have a string with this date format "DD/MM/YYYY", could >>> you please write me the code to convert it to date without the TIME. [quoted text clipped - 14 lines] > I don't have the code here with me , and since I'm not an expert I don't > remember it quite well so. I was hoping that, if I could see your code, I could guess at what your question was, and then provide the answer. Anyway, I have a theory now, but I'm not sure if this will actually give you the information you're looking for.
The DateFormat.parse() method returns a Date object, which represents some point in time, down to the millisecond precision. Now, there are many ways of writing down a point in time. One might be "12.13.52 3:30pm", another might be "January 12, 1952, 3:30:32pm", and yet another might be "Tuesday, April 12, 1952 AD 3:30:42pm PST"
I'm guessing you're taking this Date object, and then converting it to a string, and its default representation includes the time, which you don't want.
In that case, you should use the DateFormat class to specify the format you want the date to appear in! Read the documentation for that class, and you'll find out how to specify this format. Simply specify a format that doesn't involve time at all.
- Oliver
Roedy Green - 23 Mar 2006 01:48 GMT >Well, I didn't think it was that big a deal, they must be 4 lines of code. >Anyways it doens't make sense to me too, the String is '22/03/2006' and I >don't understand why it the conversion gives me also the time. >I'm using the DateFormat.parse() method. Java Calendar objects always have a time. If you want pure dates, use BigDate .see http://mindprod.com/products1.html#COMMON11.
The usual trick it so to chose midnight or midnight GMT or noon to represent the whole day. You carry the time baggage around, tripping over it, and try to hide it when you display, and try to get the timezone out the way. It is nutty, but it is the official way to handle pure dates. e.g. 2006-12-25.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 23 Mar 2006 01:50 GMT >Well, I didn't think it was that big a deal, they must be 4 lines of code. >Anyways it doens't make sense to me too, the String is '22/03/2006' and I >don't understand why it the conversion gives me also the time. >I'm using the DateFormat.parse() method. What is very puzzling about your statement is you say you converting from String to some internal form, but then talk of time tacked on tthe end, which is a property of a String. Which direction are you going?
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Jerry - 23 Mar 2006 00:12 GMT First, I assume you really meant to write "It always converts the String to Date with the time at the end.." not, "converts String to Data..."
Second, as one of the other posters said, without code, I can't tell exactly what your problem really is. In particular, I cannot tell exactly what you mean by "with the time at the end."
But I'll take a guess.
I think that what you're doing is using a DateFormat object to parse your String and return to you a java.util.Date object. Then, perhaps, you do something like print the Date object using something like System.out.print, which implicitly uses the Date class' toString method. You end up seeing a time in the output, not just what an ordinary, non-programmer would refer to as a Date. I'm going to even hazard a guess that you want to do something like compare two Dates for equality, and expect the commonsense notion that 22 March 2006 12:00 UTC is the same "date" as 22 March 2006 10:32:15 UTC, but alas, Java doesn't quite see it that way.
The problem is that java.util.Date is a poor choice of class name. This object does not represent a date; it represents a timestamp, precise to the millisecond. (Not to be confused with "accurate to the millisecond," as my college physics professor was wont to lecture us.)
What I think you want to end up with is an object in which all times between 0:00:00.000 and 23:59:59.999 inclusive are considered equal. If that is the case, you'll have to feed your date into an instance of the java.util.Calendar class, which will then allow you to manipulate the individual pieces, and set the "time" to 0:00:00.000. Then either save the result back as a Date object, or even simpler, just get the long value from the resulting Date and use that. You will always get values that are integral multiples of 86,400,000 milliseconds apart, representing midnight at the start of whatever date your data represents.
Java has no pure "Date" object. java.sql.Date is tempting, but it is a subclass of Date that sets the time value to zero, as I described. The problem is that its only non-deprecated constructor requires a timestamp in milliseconds.
I hope this helps. If not, my guesses were probably too far off the mark, so you'll have to post a better question.
Cheers! Jerry Oberle perl -e 'printf "mailto%c%c%s%cearthlink%cnet%c", 58, 103, "oberle", 64, 46, 10;'
Oliver Wong - 23 Mar 2006 16:44 GMT > What I think you want to end up with is an object in which all times > between 0:00:00.000 and 23:59:59.999 inclusive are considered equal. [quoted text clipped - 6 lines] > representing midnight at the start of whatever date your data > represents. If you're going to work with seconds-since-the-epoch, it might be simpler to immediately convert the Date to a long, and then divide by 86'400'000 (and allow integer division to do its normal truncation), then immediately re-multiply by 86'400'000.
But the problem with this kind of manipulation is that it might behave funny if internally, your code is manipulating dates using a different time zone than the one the user is expecting?
Is 1:00 AM EST on the same day as 1:00 PM EST? Well, it is in Eastern Time Zone, but the two timestamps might be in different days in other time zones.
Perhaps a more robust solution would be to write a Comparator which compares two Date objects, and it assumes that the two dates will refer to the same time zone (you should document this), and then it compares ONLY the date portion of the Date objects (i.e. it ignores the time-portion).
- Oliver
Daniele - 23 Mar 2006 20:00 GMT Well, its only a month that I''ve been looking into Java, and ENGLISH isn't even my native language, so, sorry if I'm not too good at posting questions. Anyway, thanks for your help, and cheers..
> First, I assume you really meant to write "It always converts the > String to Date with the time at the end.." not, "converts String to [quoted text clipped - 45 lines] > perl -e 'printf "mailto%c%c%s%cearthlink%cnet%c", 58, 103, "oberle", > 64, 46, 10;' Roedy Green - 23 Mar 2006 01:44 GMT >Let's say that I have a string with this date format "DD/MM/YYYY", could you >please write me the code to convert it to date without the TIME. > >I tried, but It always converts the String to Data with the time at the end, >could you please write me the code to do this ? I pointed you to the code earlier. see http://mindprod.com/jgloss/calendar.html#PRECISE
All you do is trim the tail end off the template for the stuff you don't want.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|