• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

convert data type string to timestamp

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic