Java parsing String date to MilliSecond - java

How do I convert date string 'Fri Mar 14 09:44:31 IST 2014' to millisec?
I tried with this Java code:
String dateStr = "Fri Mar 14 09:44:31 IST 2014";
SimpleDateFormat sdf = new SimpleDateFormat("MM dd HH:MM:ss");
try {
System.out.println(sdf.parse(dateStr).getTime());
} catch (ParseException e) {
e.printStackTrace();
}

your SimpleDateFormat is not correct.
Try this
SimpleDateFormat sd = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

try following code.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

Related

Error when trying to convert String to Datetime in Android

I'm new in Android and I have a problem with this. I want to convert this datetime string "EEE MMM dd HH:mm:ss Z yyyy" to "HH:mm". Here is my string:
String date = "Mon Sep 28 19:49:26 GMT+07:00 2015";
I have tried to convert it like this but it failed and I couldn't see what exception was because Exception e always null!
private String convertDate(String date) {
try {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date d = format.parse(date);
SimpleDateFormat serverFormat = new SimpleDateFormat("HH:mm");
return serverFormat.format(d);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
So how can I get "HH:mm" or how can I convert it? Does any problem in my convert function?
Locale
Set your Locale to Locale.ENGLISH.
... = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
... = new SimpleDateFormat("HH:mm", Locale.ENGLISH);

Converting String to Date throws parse exception

I am trying to convert String to Date to compare from current Date and it throws parse Exception
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
try {
java.util.Date cookiedate = format.parse("Tue Apr 29 11:40:55 GMT+04:00 2014");
Calendar currentDate = Calendar.getInstance();
String dateNow = format.format(currentDate.getTime());
java.util.Date currDate = format.parse(dateNow);
if (currDate.getTime() > cookiedate.getTime()) {
return true;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In your format, "yyyy-MM-dd HH:mm:ss" will match date string like "2013-03-30 15:57:00", so you get a ParseException.
If you want to parse "Tue Apr 29 11:40:55 GMT+04:00 2014", you should use "EEE MMM dd HH:mm:ss z yyyy" Change your code to
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
At first, you should parse the string date to Date object using EEE MMM dd HH:mm:ss Z yyyy format then convert that Date to yyyy-MM-dd HH:mm:ss format as follows...
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try {
Date cookiedate = format.parse("Tue Apr 29 11:40:55 GMT+04:00 2014");
Calendar currentDate = Calendar.getInstance();
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
cookiedate = format.parse(format.parse(cookiedate));
String dateNow = format.format(currentDate.getTime());
Date currDate = format.parse(dateNow);
if (currDate.getTime() > cookiedate.getTime()) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
I think it is pretty obvious that formatted date "Tue Apr 29 11:40:55 GMT+04:00 2014" does not match format "yyyy-MM-dd HH:mm:ss".
If you are using format "yyyy-MM-dd HH:mm:ss" you should parse strings like 2014-03-30 10"59:23.
If you want to parse string like "Tue Apr 29 11:40:55 GMT+04:00 2014" you should use format like EEE MMM dd HH:mm:ss z yyyy. (I am not sure about z, probably it should be Z).

Java date format convert on Android

I have date String as below:
Fri, 19 Jul 2013 01:30:22 GMT
Thu, 12 Sep 2013 19:19:18 GMT
And I want to convert to below format:
2013/07/19 01:30:22
2013/09/12 19:19:18
At first, I use below code to parse date:
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
But it show below exception on line Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");:
java.text.ParseException: Unparseable date: "Fri, 23 Aug 2013 01:10:35 GMT" (at offset 0)
How can I modify it?
try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
your default language is probably not English and does not accept Fri and Aug
Use this code
String dateString = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date;
try {
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
.parse(dateString);
Log.e("result", "Date:" + simple.format(date));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try this
String DateStr="Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(DateStr);
System.out.println(sim.format(d));
You can make like this.
String newDate = date.getYear()+"/"+date.getMonth()+"/"+date.getDay() and ect.
or you can write your own convert method
String myConvert(Date date)
{
return date.getYear()+"/"+date.getMonth()+"/"+date.getDay()+ect.
}
You can try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
String formattedString=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").
format(date).toString();
System.out.println(formattedString);
Try
String dateStr = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(dateStr);
System.out.println(sdf.format(date));
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
date.parse("Fri, 19 Jul 2013 01:30:22 GMT");
String newDate = simpleDateFormat.format(date);
Try with "format" :
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "";
try {
date = dateFormat.format("Fri, 23 Aug 2013 01:10:35 GMT");
} catch(Exception e){
e.printStackTrace();
}

Java Date parsing from specific String format under Android env

What's wrong with below date format? I'm getting java.text.ParseException: Unparseable date Thu, 03 May 2012 14:00:00 CEST
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
I have tried below combinations but am out of luck:
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
I'm getting the same exception if modify CEST to CET, but not for PST. Any pointer will be appreciated. Thanks.
It works for me too
package test.java;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println("Date = " + parsedDate);
}
}
And the output I get is:
Date = Thu May 03 13:00:00 BST 2012
Its working... see this..
- Please correct the typo from inputDateString to inputDate in parse()
Its a Working Java Code... Modify it to suit your Android use....
public class CopyArray {
public static void main(String[] args) {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
try {
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Aside from the minor issue that you are trying to parse inputDateString when your variable is actually called inputDate, I'm able to run your code with no exceptions.

Correct dateformat for String

I'm receiving an exception when parsing the following date:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date parsedDate=null;
try {
parsedDate=sdf.parse("Thu Jan 26 15:05:48 COT 2012");
} catch (ParseException e) {
e.printStackTrace();
}
Is the pattern incorrect? What would be the correct form to parse the date string?+
The default SimpleDateFormat constructor doesn't support all Locales.
You would have to specify the Locale:
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date parsedDate = null;
try {
parsedDate = sdf.parse("Thu Jan 26 15:05:48 COT 2012");
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Try using only one z:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Have you ever tried a single 'z' instead of a triple 'z' ?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Should become
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Perhaps you need to use CO instead of COT for the timezone?

Categories

Resources