Unable to convert String to date, Unparseable date exception - java

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 :-)

Related

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();
}

Android SimpleDateFormat always returns wrong week day

I have the following code:
String s = "08-12-2014 05:00:00"
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("EEEE, HH:mm a");
Date oneWayTripDate = null;
try {
oneWayTripDate = inputFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
String datetime = outputFormat.format(oneWayTripDate);
but for some weird reason it always returns the wrong day of the week. what am i doing wrong?
The input SimpleDateFormat pattern is wrong. Given the date 08-12-2014 05:00:00 with the year part at the end, and assuming 08 is the month, the format should be:
SimpleDateFormat inputFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH);
See the Javadoc of SimpleDateFormat for how to define date patterns.

i want to convert date fromat: "mm/dd/yyyy hh:mm" to "yyyy-mm-dd hh:mm:ss"

Response from jsp is coming in this format: "mm/dd/yyyy hh:mm", and I want to convert to db format "yyyy-mm-dd hh:mm:ss".
I tried this code :
public java.sql.Date getdateFormat(String datestring) throws ParseException {
String datestr = "";
try {
java.util.Date date = new SimpleDateFormat("mm/dd/yyyy hh:mm a",
Locale.ENGLISH).parse(datestring);
atestr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
} catch (Exception e) {
e.printStackTrace();
}
return getDateFromString(datestr);
}
public java.sql.Date getDateFromString(String string) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date finalDate = null;
try {
finalDate = sdf.parse(string);
} catch (Exception e) {
e.printStackTrace();
}
return new java.sql.Date(finalDate.getTime());
}
A common mistake in using SimpleDateFormat is skip the documentation and assume that is knows when mm means months and when mm mean minutes. It doesn't. mm only means minutes. If you want months use MM Also only use a if you expect AM/PM and only use hh for 12 hour clocks. I would expect your format should read
MM/dd/yyyy HH:mm
and your output
yyyy-MM-dd HH:mm:ss
BTW You shouldn't need to convert to a String to use JDBC. Using a Date is faster and less error prone.
If all you want is the Date then you do not need to do
atestr=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
simply return the date as this stage.
A date does not have any formatting, it is basically a number.
I would basically do it like this
String dateInString = "20140611";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date toDate = sdf.parse(dateInString);
sdf = new SimpleDateFormat("yyyy-MM-dd");
String tmpStr = String.format(sdf.format(toDate));
System.out.println(tmpStr);
We can get the date in the following way
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parseDate = dfm.parse(datestring);
Now the parseDate is in the format "yyyy-MM-dd HH:mm:ss"

Parse Datetime to JAVA Date

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

Convert EST time to local Time in java

I'm unable to convert (12/19/2012 8:57am EST) to local Time (now Indian Time).
While converting I'm getting wrong time (Dec 19 2012 11:27). I'm using the following code:
private void convertEdtToLocalTime(String pubDate)
{
//pubDate = 12/19/2012 8:57am EST;
String localPubDate;
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy HH:mma z");
TimeZone timeZone = TimeZone.getDefault();
sdf.setTimeZone(timeZone);
if (pubDate != null)
{
Date date = sdf.parse(pubDate);
sdf = new SimpleDateFormat("MMM dd yyyy HH:mm");
localPubDate = sdf.format(date);
}
}
catch (ParseException e)
{
}
}
You dont need to set the timeZone as the the time zone is already specified in the pubDate string. When you want to format using different SDF, the default timezone will convert it into default timezone itself. For eg. if you are in india, IST time = Dec 19 2012 19:27
private static void convertEdtToLocalTime(String pubDate)
{
//pubDate = 12/19/2012 8:57am EST;
String localPubDate=null;
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy HH:mma z");
// TimeZone timeZone = TimeZone.getDefault(); // No need to do it
// sdf.setTimeZone(timeZone);
if (pubDate != null)
{
Date date = sdf.parse(pubDate);
sdf = new SimpleDateFormat("MMM dd yyyy HH:mm");
localPubDate = sdf.format(date);
}
}
catch (ParseException e)
{
}
System.out.println(localPubDate);
}

Categories

Resources