Parse Datetime to JAVA Date - java

im getting the SQL Server datetime (1 Jan 2013) form the SqlRowSet like
while (rs.next()) {
myBean.setDateProp(rs.getString(4));
}
the type of myBean DateProp is java.util.Date, is there a way to convert (1 Jan 2013) to java Date representation.
i have tried the following code
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
Date date=new Date();
try {
date = sdf.parse("1 Jan 2013");
} catch (Exception e) {
e.printStackTrace();
}
and i get the ParseException
SEVERE: java.text.ParseException: Unparseable date: "1 Jan 2013"
any directions...

try this -
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
Date date=new Date();
try {
date = sdf.parse("1 Jan 2013");
}catch (Exception e) {
e.printStackTrace();
}

Try
while (rs.next()) {
myBean.setDateProp(rs.getDate(4));
}

#Test
public void test() throws ParseException {
String dateString = "1 Jan 2013";
String dateString2 = "11 Jan 2013";
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
sdf.parse(dateString);
sdf.parse(dateString2);
}

You need to modify your Date formatter string to
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
as your date string is in this format

Related

Unable to convert String to date, Unparseable date exception

I am passing dateInString as "2015-07-30T14:30:00-04:00". But
it is throwing
java.text.ParseException: Unparseable date:
"2015-07-30T14:30:00-04:00" (at offset 0)
Here is the sample code:
private Date convertStringToDate(String dateInString){
if(dateInString!=null){
Date convertedDate=null;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
TimeZone timeZone = TimeZone.getDefault();
formatter.setTimeZone(timeZone);
try {
convertedDate = formatter.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
else
return null;
}
I have solved it. Here is the date format-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Thanks everyone for your time :-)

Unparseable date error in Java

I want to convert the system date to yyyy-MM-dd format. There are similar questions in SO. I found that I need to parse the date in input format and then convert to the output format. But I am stuck at the first stage itself. I am not able to parse the system date as such (Sat Apr 25 14:44:15 IST 2015).
Here is my MWE:
import java.util.*;
import java.text.*;
public class Test
{
public static void main(String[] args)
{
try
{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY");
date = dateFormat.parse(date.toString());
System.out.println(date);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I get the exception as :
Unparseable date: "Sat Apr 25 14:53:33 IST 2015"
Date object can be converted to string of any date format.
String can be converted to date but it will come only in standard date format's but cant be in the one as you want..
If you want to format system date to yyyy-MM-dd format then use:
Date date = new Date();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
String date1 = dateFormater.format(date);
As you specified in comment you want to subtract sql date with current date then just convert the sql date to normal date format.
Like this:
String date = your date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(date);
Date currentdate = new Date();
Then use calender objects:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
Calendar calendar2 = calendar.getInstance();
calendar2.setTime(currentdate);
long difference = (calendar2.getTimeInMillis() - calendar
.getTimeInMillis()) / 60000;
This will give you the difference between two dates in minutes.
This will work for you
public class Test1 {
public static void main(String[] args) {
try
{
Date date = new Date();
System.out.println(date);
String dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY").format(date);
System.out.println(dateFormat);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Output
Sat Apr 25 15:10:38 IST 2015
Sat 04 25 15:10:38 PM 2015
I think you should do it like that.
Date date = new Date();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
date = format.parse(formattedDate);
System.out.println(date);
But you should understand difference between "date" and "date format".

Android Date format. from string to Date

I have a string like this: 2015-01-31 16:00:00 +0000 UTC, I want to parse this string to date object.
yyyy-MM-dd HH:mm:ss 'UTC'
but failed.
You can use the SimpleDateFormat for formatting the strings to date objects:
DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+z", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = originalFormat.parse("2015-01-31 16:00:00 +0000");
String formattedDate = targetFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}

How to convert date string to Date data type [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have a
String dateString = "Fri Feb 14 00:00:00 IST 2014";
I need output in Date datatype like 2014-02-14.
Here is the code which is throwing Parse exception.
Need help in this.
public static void main(String args[]){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date convertedDate = null;
try {
convertedDate = df.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
}
Base point is - Input string should match with date pattern
Raised parse exception as becasue wrong pattern, use this date pattern - EEE MMM dd hh:mm:ss z yyyy
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
You have to convert dateString to matching Date format and then you can format that Date what ever the format you want.
Try this
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat df2 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date date=df2.parse(dateString); // convert stringDate to matching Date format
System.out.println(df1.format(date));
Out put:
2014-02-14 12:00:00
Try this:
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date convertedDate = null;
try {
convertedDate = df.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
}

SimpleDateFormat.parse()

SimpleDateFormat.parse() accepts the date 003/1/2011 when the format is MM/dd/yyyy. Trying with code below:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2;
try
{
dt2 = sdf.parse(_datemmddyyyy);
}
catch (ParseException e)
{
return false;
}
and the date is parsed as 00/11/2011. What is wrong?
Are you sure? This:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2 = sdf.parse("003/1/2011");
System.out.println(dt2);
Yields:
Tue Mar 01 00:00:00 PST 2011
That's 03/01/2001 in MM/dd/yyyy. Seems right?

Categories

Resources