Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Get A Date as a String in java  RSS feed

 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,all you might say this is repeated question but before asking i went to javaranch questions i didnt get what i wanted thats why i am asking here again
i am setting date as follows

what i want is instead of that new Date() i want to pass my own date then how to proceed for that
and getter and setter is


if you dont understand my question then please forgive me either correct me or give me some solution please
advance thank you for your valuable time
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your topic title "How To Get A Date as a String in java" contradicts with "what i want is instead of that new Date() i want to pass my own date then how to proceed for that "
So what exactly are you trying to do here?
 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i want is i want to pass a new Date instead of current date but e.setDate(newDate()) says it accepts only date so what to do
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to create some specific date. Did you check the API docs for options available to you for constructing a new date object?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshal deshpande wrote:what i want is i want to pass a new Date instead of current date but e.setDate(newDate()) says it accepts only date so what to do


I suspect you want to construct a java.util.Date from a String. If this is correct then you should most probably use class SimpleDateFormat with method parse(). The Javadoc for SimpleDateFormat gives details.
 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir,i really read that and even solved some examples on using SimpleDateFormat but i am not able to implement it in this application.if you want i can show you the examples which i tried but really am stuck here.when i tried to use parse() method it says we only accept String cant parse date
 
Suresh Sajja
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give the code where you are getting exception when you used with java.lang.SimpleDateFormat
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us your SSCCE code where you are trying to create a date from a given string
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshal deshpande wrote:sir,i really read that and even solved some examples on using SimpleDateFormat but i am not able to implement it in this application.if you want i can show you the examples which i tried but really am stuck here.when i tried to use parse() method it says we only accept String cant parse date


Sorry but I obviously don't understand. I assumed you needed a java.util.Date object to use as the argument to a method but that you only had a String version (for example "23-11-2012 18:43:22") and for this you need to use SimpleDateFormat.parse(the string). It now seems you need to create a String from a java.util.Date object in which case you need to use SimpleDateFormat.format(the date as a java.util.Date object) . Again you need to read the Javadoc for SimpleDateFormat .
 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the program which i understood but i dont know how to use this for my application
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshal deshpande wrote: i dont know how to use this for my application


Sorry but I don't see how we can help. You don't say how the date is presented to you! You just show a setter that takes a java.util.Date object.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain in detail, step by step, what exactly you want. Because it is currently not clear what your question is. We can guess and give you an answer based on our guess, but you won't be happy with that if our guess of what you want is not what you meant.

Here is my guess: You ask the user to input a date, and the user enters it as a string. You want to make a java.util.Date object from the string, and pass it to the setDate() method of your Event object. Is that right? If that's it, then you should use a SimpleDateFormat object to parse the string into a Date object. You can then pass that Date object to the setDate() method of your Event object.
 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes exactly jesper sir said thats the problem i have thank you sir i will try to get solution using that steps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!