Home > Default > Convert a string to a Date

Convert a string to a Date

October 11Hits:0
Advertisement
I have a string object.
String s = "2006-12-26 17:33:36"
How can I create a date object from that string?
I tried Date date = new Date(s);
But that looks like a deprecated method. What is the best way ?
Im using mysql db and their the type of the column is datetime. But I can store their if I can convert to Date type. Can you help me.
thanks in advance..

Answers

Look at the parse() method of thge SimpleDateFormat class. In the javadoc, it says this;
parse
public Date parse(String text, ParsePosition pos)
Parses text from a string to produce a Date.
The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed date is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Read other 7 answers

Tags:

Related Articles

  • How to convert a string value to dateOctober 11

    Dear All, I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date. but when i try to convert the variable to date value, I am getting the error as below. Please h

  • Error in converting character string to smalldatetime data typeOctober 11

    I've installed SQL Server 2000 on my new laptop and Crystal Report XI, out of sudden, the Crystal Report to certain view can't run and I got the following error: Details: 22007 [Microsoft][ODBC SQL Server Driver][SQL SERVER]Syntax Error cnverting cha

  • How to convert a string into orcle date formatNovember 30

    i need some information about converting string into date format.i have string like '20-SEP-05' .so we have to convert into date formate like yy-mm-dd' (2005-09-20).how can we do it.Here's some code to help. I can't remember what method uses what for

  • Converting 3 Strings to 1 Date?November 30

    Hi first of i am very new at this sql. But my problem is that i am haveing 3 combo boxes in java. 1 with dd 1 with mm and 1 with yyyy. How do i convert these 3 strings into 1 date format? Writing the Gui in java. It is to a booking system so i need i

  • Converting a string into mySQL date formatOctober 11

    I have a bit of code that collects data values from a bean and I want it to then insert this into a mySQL database. My string is in the format "23/12/1983", how can i put this into a date field in my database? The bit of code I have got so far l

  • Convert a string to a DateOctober 11

    I have a string object. String s = "2006-12-26 17:33:36" How can I create a date object from that string? I tried Date date = new Date(s); But that looks like a deprecated method. What is the best way ? Im using mysql db and their the type of th

  • Convert a string in a dateNovember 30

    i have a string dat1="01/01/01/" i want to convert it to a date (type Date) do so know how to do ????? please help thanks for allPlease don't crosspost. It cuts down on the effectiveness of responses, leads to people wasting their time answering

  • How to convert a String into a date in yyyy-MM-dd using SimpleDateFormat?October 11

    Hi Guys, I am using the following code import java.text.ParseException; import java.text.SimpleDateFormat; public class ValiDATE {      private static boolean validateDateFormat(String strDate) {           try {                SimpleDateFormat format

  • Converting the string value to data formatNovember 30

    Hello Everyone,                               Please guide me in converting the value to date format. from source i'm getting the value (which is acchally a data value) '20070730'. I need this value to be in date format '2007/07/30' Please help me in

  • How to convert a string to a Date obj ?November 30

    Hi, String xx; "xx is from a table rs.getString();" Date dd = Date.valueOf(xx); id = today.compareTo(dd); Complier complains , why ? Thanks in advance. PhilipHi, I finally figured the reason. I have to put "java.sql.DatevalueOf( )" in

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.504 s.