aspose file tools*
The moose likes Beginning Java and the fly likes convert data type string to timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "convert data type string to timestamp" Watch "convert data type string to timestamp" New topic
Author

convert data type string to timestamp

elli dian
Greenhorn

Joined: May 18, 2006
Posts: 15
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

Joined: Mar 22, 2006
Posts: 48
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!


to err is human, but the company policy doesn't allow it!
elli dian
Greenhorn

Joined: May 18, 2006
Posts: 15
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

Joined: Mar 22, 2006
Posts: 48
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

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39387
    
  28
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

Joined: Mar 22, 2006
Posts: 48
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

Joined: Jan 28, 2010
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert data type string to timestamp