File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert data type string to timestamp

 
elli dian
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to convert data type string to timestamp?
For example;
String fdate="2006-05-22 14:04:59:612";
So I want to convert data type of fdate to timestamp.
Give me a solution!!!
 
mohit bahl
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

here is something you can use

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
java.util.Date parsedDate = sdf.parse("2006-05-22 14:04:59:612");
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());

cheers!
 
elli dian
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried your code but i get error.
JavaCompile: SimpleDateFormat cannot be resolved or is not a type.
and what do you mean sdf?
Please give me a solution again!!
 
mohit bahl
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops small mistake

use this

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
java.util.Date parsedDate = dateFormat.parse("2006-05-22 14:04:59:612");
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

and also put this code in try catch block to catch java.text.ParseException

try this
Cheers
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by elli dian:
...Please give me a solution again!!

Whoa, calm down buddy. Have you looked at the documentation for the Simple DateFormat that mohit was referring to? Although there may have been a slight syntax error, you were definitely steered in the right direction.
[ May 29, 2006: Message edited by: Garrett Rowe ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to find a list of error messages. Try here.
"Cannot be resolved" probably means you haven't imported the SimpleDateFormat.

You need to find which package it is in. Go to the API specification, here, click "FRAMES," then find the name of your class in the lower left frame. To get details click on SomeClass, but if you just let the mouse hover over the name, a box appears with "Class in java.somepackage" in.
then writeORRemember the import declarations come one line before the name of the class.
 
mohit bahl
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is one more thing that you will have to change if you are trying to convert a string "2006-05-22 14:04:59:612" then the format you will use should be exactly the same "yyyy-MM-dd HH:mm:ss:SSS" other wise you will get a parse exception.

and if you ask for a solution you will get one but i think you want a fully running code which you can just copy paste and run, where as you should refer to the pointers given in this forum and then refer to java api documentation to understand what is actually going on in those functions.

i hope you understand what i have pointed out here.

cheers!
 
Eliza sahoo
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below are additional examples of conversion from one data type to another in java.

double to String :
String str = Double.toString(i);

long to String :
String str = Long.toString(l);

float to String :
String str = Float.tString(f);

String to integer : str = "25";
int i = Integer.valueOf(str).intValue();
or
int i = Integer.parseInt(str);

String to double :
Double d = Double.valueOf(str).doubleValue();

String to long:
long l = Long.valueOf(str).longValue();
or
Long L = Long.parseLong(str);

String to float :
Float f = Float.valueOf(str).floatValue();

decimal to binary : int i = 42;
String bin = Integer.toBinaryString(i);

http://www.mindfiresolutions.com/Converting-datatype-in-Java-46.php
 
Rob Spoor
Sheriff
Pie
Posts: 20393
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For anything to String conversion you can just use one of the many String.valueOf methods. These will call Integer.toString, Long.toString etc, but it's easy to have one single entry point.

String to any primitive can be done using the <Wrapper>.parse<type> methods, e.g. Integer.parseInt, but also Double.parseDouble and Boolean.parseBoolean. Character / char is the only exception but that's easy enough: just check for the length and take str.charAt(0).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic