• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Unable to get Date instance in a particular format

 
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to get a date instance in "EEE MMM dd HH:mm:ss.SSS zzz" yyyy format.
I am getting it in String format but when I am trying to parse it to Date format , I am not getting the required.I am missing the milisecond pattern.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am missing the milisecond pattern.


Missing ? What do you mean by missing ? Can you give us an example of your output ?
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sample code -



And the output I am getting as -
date_set Wed Jul 21 12:07:43 IST 2010
value Wed Jul 21 12:07:43.113 IST 2010


I think I am doing something wrong. So please provide me the correct way to achieve the data instance as Wed Jul 21 12:07:43.113 IST 2010
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Date objects do not have a format by themselves. So you cannot "get a Date instance in a particular format". A Date object is nothing more than a number of milliseconds since 1 January 1970, 00:00:00 GMT.

When you display a Date by calling toString() on it (explicitly or implicitly) as you are doing in line 9 of your code, then it will use some default format to display the date. The Date object does not remember how you created it, by parsing a string with a certain format.

If you want to display a Date with a specific format, you should use DateFormat.format(...) to convert it to a String.

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that but I want to return the value in Date format only.
I tried to convert the String to Date and thereby I was not able to get the milisecond.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are printing a Date. If you'd print the "String value" instead, you'd see that the milliseconds are not lost. Date do not hold any format, so when you call System.out.println on a Date, Date#toString will be called. If you look at Date#toString API, it says :


Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy


That's why you don't see any milliseconds.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any possible way to achieve the required output only in date format?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't seem to understand the main concept. Date instances do not have any format. To prove you that the millisecs are not lost in the Date instance, try this after your System.out.println :


Do you want the date to be a String, or a Date ?
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want it in Date.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your program is doing this:

01. Create a DateFormat object with the pattern "EEE MMM dd HH:mm:ss.SSS zzz yyyy"
02. Create a Calendar
03. Set the Calendar to the current date and time (not necessary because it is already set to the current date and time)

05. Format the current date and time of the Calendar to a String with the format "EEE MMM dd HH:mm:ss.SSS zzz yyyy"

08. Parse the string back to a Date object (why? you could just as well have done Date date_set = Calendar.getTime())
09. Print the Date object by calling toString() on it implicitly

You seem to expect that the Date object you create in line 8 remembers the format. It doesn't. Date objects do not know anything about formats. When you print the Date object in line 9, some fixed, default format will be used. Not the format "EEE MMM dd HH:mm:ss.SSS zzz yyyy" because the Date object doesn't know that it was created using that format string.

The fixed, default format doesn't include the milliseconds. If you need to display the milliseconds, then you'll need to format the Date object yourself using an appropriate DateFormat object, instead of calling toString() (explicitly or implicitly) on the Date object.

buntha Choudhary wrote:I want it in Date.


If you expect the Date object to be able to format itself displaying milliseconds, then that's not possible, as we're trying to explain to you.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone...thanks for discussin and answerin...

i wanted to pass a date object in a particular format to a method...like "yyyymmdd"...searchin the forum got this thread and so can i conclude that date object cannot be created in a particaluar format...?? need to pass it to a third party method, who is expectin date in this format..(yyyymmdd)..please note i dont want a String data type...i want it to be in a date data type.

please correct me if my understanding is wrong..

thanks and cheers...

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already mentioned multiple times, a Date object doesn't know anything about date formats. It's not possible to have a Date object in "yyyymmdd" format - there's no such thing as a Date object with a format.
 
Naveen Kumar Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep...thanks...!!
 
permaculture is largely about replacing oil with people. And one tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic