SimpleDateFormat parsing troubles - android

I'm having trouble parsing a date that I'm trying to reformat. The SimpleDateFormat is giving me a real headache.
I'm getting this date from a news feed:
Wed, 06 Mar 2013 09:22:00 +0100
And I need to format it to this:
06.03.2013
I could just use a hashmap with all the months but I would like to use the SimpleDateFormat, since that's what it's for.
But I can't seem to find the right pattern.

Try adding following code snippet:--
//SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS");
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS", Locale.ENGLISH);
Date date = null;
try {
date = fmt.parse("Wed, 06 Mar 2013 09:22:00 +0100");//Hardcoded for your case...
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat fmtOut = new SimpleDateFormat("dd.MM.yyyy");
System.out.println("Date :-- " +fmtOut.format(date));

Related

Convert Mon Sep 17 05:52:14 EDT 2018 type date format to simple Date and Time android

I am currently working on a chat application by using Firestore. When I try to get Timestamp from firestore Document with
Date date=documentSnapshot.getDate(fieldvalue1,DocumentSnapshot.ServerTimestampBehavior.ESTIMATE);
then it returns to date in "Mon Sep 17 05:52:14 EDT 2018" this format.I am not able to convert that date into Simple date format. Please help.
You can use EEE MMM dd HH:mm:ss z yyyy pattern for retrieve date from string. Like,
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try {
Date date2 = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
EDIT
You can convert into Indian time Like,
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try {
format.setTimeZone(TimeZone.getTimeZone("GMT-04:00")); //for EDT
Date date2 = format.parse(dateStr);
TimeZone tz = TimeZone.getTimeZone("GMT+5:30"); //for Indian Time
format.setTimeZone(tz);
String result = format.format(date2);
} catch (ParseException e) {
e.printStackTrace();
}

Android Convert DateTime to EEE, d MMM yyyy

I am trying to convert the following data time to EEE, d MMM yyyy format but kept getting Unparsable date format. Can some one please help. I couldn't find anything on the web that helped.
Here is the code
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date convertedDateStart = new Date();
try {
convertedDateStart = dateFormat.parse(datestr);
camp_new.startdate = convertedDateStart;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help wound be greatly appreciated. Thanks in advance
The date string "2017-01-12T00:00:00Z" cannot be parsed with your format "EEE, d MMM yyyy HH:mm:ss Z"; the fields are the wrong type or in the wrong place.
The format for your datestr "2017-01-12T00:00:00Z" should be:
yyyy-MM-dd'T'HH:mm:ssZ
You need to initialize the SimpleDateFormat with this format to parse datestr.
The format "EEE, d MMM yyyy HH:mm:ss Z" would parse a date that looks like
Thu, 1 Jan 2017 00:00:00 Z
Read the docs to understand the difference between parse() and format().
You have to parse it first, store it as a Date object and then use another SimpleDateFormat to format in the desired way.
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = dateFormat.parse(datestr);
String convertedDateStart = dateFormat2.format(date);
Well This will Work For you if you want to FORMAT and get a Date String with that format..
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
try {
date = dateFormat.parse(datestr);
} catch (ParseException e) {
e.printStackTrace();
}
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
datestr = dateFormat.format(date);
System.out.println(datestr);
This will give you Thu, 12 Jan 2017 00:00:00 +0430 Date String.

Parsing date using SimpleDateFormat to get Time

I am working on an Android application in which I need to parse my date to get my time. I am getting parsing exception when I am using my below code.
Here is the code:
SimpleDateFormat parseFormat = new SimpleDateFormat("dd MM hh:mm:ss yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try { //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
date = parseFormat.parse(selectedDateTime+"");
System.out.println(printFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the exception:
java.text.ParseException: Unparseable date: "Fri Jan 15 09:30:00 GMT+04:00 2016" (at offset 0)
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Your formatting is off, try this.
Your Strings are wrong. The correct ones are:
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("HH:mm:ss");
I guess your initial date format is wrong. Can you share the value of selectedDateTime.
I guess it in different format (as per your comment) like Fri Jan 15 09:30:44 GMT+04:00 2016
If so please try this code
SimpleDateFormat parseFormat = new SimpleDateFormat("E MM dd hh:mm:ss z yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try { //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
date = parseFormat.parse(selectedDateTime+"");
System.out.println(printFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
update
try {
String dateTest = "Fri Jan 15 09:30:00 GMT+04:00 2016";
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss zzz yyyy");
java.util.Date d = (java.util.Date) formatter.parse(dateTest);
System.out.println("" + d);
SimpleDateFormat print = new SimpleDateFormat("hh:mm:ss");
System.out.println(print.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
output
Fri Jan 15 11:00:00 IST 2016
11:00:00
Following code can help you understand how exactly formatting syntax change for date formatting
Calendar calendar = Calendar.getInstance();
System.out.println("Date: \t"+calendar.getTimeInMillis());
String dateAsText_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(""+calendar.getTimeInMillis())));
Date date = new Date(Long.parseLong(""+calendar.getTimeInMillis()));
System.out.println(""+dateAsText_);
SimpleDateFormat ft = new SimpleDateFormat ("dd-MMM-yyyy");
System.out.println(""+ft.format(date));
SimpleDateFormat time_ft = new SimpleDateFormat ("hh:mm aa");
System.out.println(""+time_ft.format(date));
Output
Date: 1452766759531
2016-01-14 15:49:19
14-Jan-2016
03:49 PM

Unable to get date in the appropriate format in android

I am obtaining the Date and converting into the GMT format. I am obtaining it in the form of Thu Jul 24 06:55:56 GMT+05:30 2014. I want the date to be displayed in the following form 6/19/2014 12:28:44 PM. Can anyone tell me step by step how to do it. I read the following document http://developer.android.com/reference/java/text/SimpleDateFormat.html but the format tends to remain the same even if I use a and L. I am posting the code below, please guide me.
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
//Time in GMT
try {
dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
Log.i("gmt time",""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
date_edittext.setText(""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
use this :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
say if it works :)
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
String currentDate = format.format(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse(currentDate));
I don't quite get your problem but look :
Date test = new Date();
test.setHours(10);
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy HH:mm:ss a");
System.out.println(sdf.format(test));
This displays 7/24/2014 10:13:01 AM
If I change setHours to test.setHours(22) this displays 7/24/2014 10:13:01 PM
Hope this helps.

Converting String value date into Date using simple date format in android

I have a string variable which contains date and time in the following format
EDIT:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
I need to covert and parse it into this date format using simpledateformat as below:
Date nd=04-03-2014 16:58:00
But I don't know the string pattern for converting using simpledateformat.
I have given as
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-dd-MM hh:mm:ss");
But it gave me wrong output.
Is the string pattern for simpledateformat sdf correct? If I'm wrong can someone please correct me in converting newdate to date nd format.
Try to use
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
But before conversion replace "." on ":" in "GMT+05.30"
Example:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
try {
Date d = sdf.parse(newdate);
} catch (ParseException e) {
e.printStackTrace();
}
Try
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-YYYY hh:mm:ss");
Your date format is not correct.
Try:
"Tue Mar 04 16:58:00 GMT+05:30 2014";
instead of:
"Tue Mar 04 16:58:00 GMT+05.30 2014";
It seems that SimplaDateFormat this GMT+05:30 part works only with ':'.
Than you can parse it like that:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" );
Date nd = sdf.parse(newdate);
First of all, your date string is not correct, which should be
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
not this...
String newdate="Tue Mar 04 16:58:00 GMT+05.30 2014";
you placed Dot (.) in GMT+05.30 instead of Colon (:) as GMT+05:30.
Now, Use this format...
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For Exmaple:
Date date;
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
date = formatter.parse(newdate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String convertedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(date);
Log.i("ConvertedDate", convertedDate);
OutPut:
03-04 19:39:49.959: I/ConvertedDate(310): 04-03-2014 17:28

Categories

Resources