• Post Reply Bookmark Topic Watch Topic
  • New Topic

to convert timestamp into date and time  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have variable name timestamp which contains date and time for an entered date . Now i want to convert timestamp into 2 separate parts i:e date and time. I have following code for that


Problem is that by this code i am getting separate parts of timestamp into date and time but it is not giving me exact output as i want when i am printing values for timestamp it is giving me correct output but after splitting i am not getting , what i am getting is here only 1st entered date and time for all the records.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why are you splitting it into parts?

Unrelated to your problem, you should stop using Calendar and Date.

Java now has the java.time package which has much easier classes to work with, such as LocalDate, LocalTime and LocalDateTime.
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is basic requirement of my project to print as a time and date .I dont want current date and time i have entered date and timestamp should be of 2 mins difference i am getting all this except the separated date and time Any modifications in this code to get my output.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem is that you're working with an unintuitive API and using String manipulation and magic values.

Learn and use the java.time API. Timestamp has a toLocalDateTime() method. LocalDateTime has various plus() methods, and you can compare LocalDateTimes with isBefore() and isAfter().
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pooja rathi wrote:I have variable name timestamp which contains date and time for an entered date . Now i want to convert timestamp into 2 separate parts i:e date and time. I have following code for that

OK, well first: are you referring to java.sql.Timestamp, java.sql.Date, and java.sql.Time?

Because if so, that's way too much code.

1. If you want to convert it to a java.sql.Date
  Date date = new Date(timestamp.getTime());

2. If you want to convert it to a java.sql.Time
  Time time = new Time(timestamp.getTime());

3. And since a Timestamp already IS a java.util.Date,you don't have to do to anything to use it as one.

HIH

Winston
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check my full code i have edited
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have reverted your edit. Please post your new code in a new post, otherwise it messes up the flow of the topic.

Another thing, please use quote tags to quote things that have already been said in past messages, not new things you're writing.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!