Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert database date object in to java string object

 
ranjith nelanti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
the date value is stored as "1344449314086" in database. that means this number represents some date.
how to convert this date in to actual date in java. is this stored in UTF-8 ???

is there any sql query or java code to convert this date. ???
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ranjith nelanti wrote:Hi,
the date value is stored as "1344449314086" in database. that means this number represents some date.
how to convert this date in to actual date in java. is this stored in UTF-8 ???

That isn't something we can answer. You should go to whoever/whatever put that information into the database and ask them how it gets stored. It could be a simple offset from a fixed point in time.

is there any sql query or java code to convert this date. ???


ResultSet#getDate(String columnName) or one of its sister-methods. Assuming the value is in the correct format.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Java, and other software, stores timestamps as a number of milliseconds since 01-01-1970, 00:00:00 UTC. The number you have looks like such a timestamp. You can simply pass that number to the constructor of java.util.Date, and then you'll get a Date object that represents that date (it seems to be 08-08-2012, 20:08:34 in my local timezone).

This does not have anything to do with UTF-8 or any other character encoding.
 
ranjith nelanti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.

Java, and other software, stores timestamps as a number of milliseconds since 01-01-1970, 00:00:00 UTC. The number you have looks like such a timestamp. You can simply pass that number to the constructor of java.util.Date, and then you'll get a Date object that represents that date (it seems to be 08-08-2012, 20:08:34 in my local timezone).

This does not have anything to do with UTF-8 or any other character encoding.


thanks for your reply. i already tried this, but if i pass this number as "Long", i am getting out of range exception, or if i pass it as a string, i am getting "java.lang.IllegalArgumentException"...can you please tell me how did you do this.

Date date = new Date(1344449314086); this is not working in my case ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try 1344449314086L. An integer constant without a qualifier is an int. To make the expression be of type long, you have to add an L at the end. Since 1344449314086 is too big for an int, it fails without the L. (It won't automatically assume you mean it to be a long. You have to tell it.)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ranjith nelanti wrote:Date date = new Date(1344449314086); this is not working in my case ?

That is using an int, not a long, so you are running past the maximum value for int. You need to use a long, so you should add L to the end of the number:
 
ranjith nelanti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
ranjith nelanti wrote:Date date = new Date(1344449314086); this is not working in my case ?

That is using an int, not a long, so you are running past the maximum value for int. You need to use a long, so you should add L to the end of the number:

great.....worked like a charm..i have been working on this since morning.....glad to you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic