I have to get time from entire date
e.g. time=11:00:00 from date 2012-09-01 11:00:00.0
I tried following snippet but getting error Error : Unparseable date: "2012-9-1.13.30. 0. 0"
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");
String outputString = outputFormat.format(date);
Edit: Now I am getting only date instead I want only time
if (iResultSet1.getDate(i) != null) {
Date date = iResultSet1.getDate(i);
System.out.println("date-->" + date);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat(
"HH:mm:ss");
String outputString = outputFormat.format(date);
// System.out.println("date1-->"+date1);
I wil suggest, in this case rather doing parsing and manipulation in java change your SQL to format and return only date as
Example
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
You are probably retrieving your date from the database (iResultSet1.getString(i)) and the problem is that you're getting wrong format, i.e. 2012-9-1.13.30. 0. 0. Either change the date format in the database or use:
Date date = iResultSet1.getDate(i);
instead of
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
Related
i have a common doubt for a long while. During date formation if the input date in the format like "2019/05/22 02:00:23" then with the help following line we can format the date,
String inputDate = "2019/05/22 02:00:23";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
I can see both input format and the required format is same. Suppose if i change the input date like below, it is showing the java.text.ParseException: Unparseable date: exception.
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
Please suggest how to achieve this.
Please try below i mentioned date format code,I hope it will help you,
From this,
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
To Change:
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.9999");
String formattedDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
System.out.println("formattedDate :: " + formattedDate);
Thanks,.
I want to change date format which I received reading from excel cell file is "30-mar-2016" to 03/30/2016. I have tried below
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate;
startDate = df.parse(inputDate);
It gave me- java.text.ParseException: Unparseable date: "30-mar-2016"
.
I also tried below code
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
String startDate;
startDate = df.format(inputDate);
getting -- java.lang.IllegalArgumentException: Cannot format given Object as a Date
Can anyone help me .
Your input string 30-mar-2016 is in the format dd-MMM-yyyy. Your output format is MM/dd/yyyy. So you need two DateForamts. One for parsing original input string, one for formatting output string.
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy"); // for parsing input
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy"); // for formatting output
String inputDate = "30-mar-2016";
Date d = df1.parse(inputDate);
String outputDate = df2.format(d); // => "03/30/2016"
I have resolved using below
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse("30-mar-2016");
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));//got 03/30/2016
Thanks everyone for your quick response
if(sqlData.get(i) != null){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.sql.Timestamp myDate = new java.sql.Timestamp(dateFormat.parse(sqlData.get(i)).getTime());
preparedStatement.setTimestamp(i+1, myDate);
I'm not sure what i'm doign wrong and i keep geting "java.text.ParseException: Unparseable date:" on the 3rd line.
These are dates that i have pulled out of a database, and are set in the past.
System.out.println(sqlData.get(i));
prints out : 2014-04-19 05:48:22
java.text.ParseException: Unparseable date
is thrown from dateFormat.parse( when the string passed to it is not in the same format as the specified pattern.
Also note that you have not posted the entire exception message (the entire line). It would have been
java.text.ParseException: Unparseable date:2014-04-19 05:48:22
In your case
2014-04-19 05:48:22 is not in the format "yyyy/MM/dd HH:mm:ss"
Your pattern string should be
yyyy-MM-dd HH:mm:ss Note the - instead of /
Here's an example of parsing a date and then creating a timestamp out of it. This was testing and is working, so if you run into any errors, it may have something to do with the data you have and not the code itself.
String dateString = "1999/10/24 10:24:99";
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateObj = dateFormat.parse(dateString);
Timestamp timeStamp = new Timestamp(dateObj.getTime());
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+d);
I get the output as
date in controller Mon Dec 31 16:04:57 IST 2012
Please suggest a method to output the date in MM/dd/yyyy HH:mm:ss format.
Need the output in date format and not as string so as to store the output in datetime field in mysql db
Need the output in date format and not as string so as to store the output in datetime field in mysql db
After the statement
Date d = (Date)formatter.parse(dateTime);
java.sql.Date sqldate = new java.sql.Date(d.getTime())
you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).
However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date# but the toString printed in user readable format thats why the confusion.
You are using d an object of Date class, so its toString method is called that gives the output as Mon Dec 31 16:04:57 IST 2012.
If you want to display it in the format that you have specified in your SimpleDateFormat then try using this :
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+ formatter.format(d));
Don't see why the single-qoutes (') are used in the format-string and you also need to catch ParseException:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d = new Date();
try {
d = (Date)formatter.parse("12/31/2012 12:13:14");
} catch(ParseException pex) { System.out.println(pex.getMessage()); }
// convert the date into java.sql.Date
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// then put it in the database, something like this:
//resultSet.updateDate("myDateTimeField", sqldate);
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
System.out.println("date in controller"+ df.format(d));
Try using format instead of parse.
Date date = new Date();
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date) );
I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another