Converting string to java.util.date -
i trying convert string in java.util.date having following errors:
helloworld.java:10: error: incompatible types: date cannot converted string return formatter.parse(date); ^ helloworld.java:16: error: incompatible types: string cannot converted date date date = convertstringtodate("2015-08-03 09:19:00.000");
my code below:
import java.text.simpledateformat; import java.util.date; public class helloworld{ private static final simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss"); public static string convertstringtodate(string date) { if(date!=null){ return formatter.parse(date); } return null; } public static void main(string []args){ date date = convertstringtodate("2015-08-03 09:19:00.000"); system.out.println(date); } }
your specified format not matching data format.
format "yyyy-mm-dd hh:mm:ss"
not compatible data "2015-08-03 09:19:00.000"
because of 2 reason: (1) am/pm missing in date 'hh' takes 1-12 hrs , , (2) milliseconds present in date string
replace below mentioned line , issue resolved.
private static final simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss");
but, none of above mentioned issue can cause exception got milliseconds ignored default.
possible reasons of exception return type of method "convertstringtodate" or hh
having value beyond range between01
, 12
.
- change return type
string
date
- change date format
hh
hh
.
Comments
Post a Comment