Android Convert DateTime to EEE, d MMM yyyy - android

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.

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

From timestamp to Date Android

Good day, I have timestamp 1481709600 and I would like to have this time format Wed, 14 Dec 2016
I'm trying to do that using:
private String getDateFromTimeStamp(Integer dt) {
Date date = new Date (dt);
return new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy ").format(date);
}
but the current output is Sun Jan 18 05:35:09 GMT+02:00 1970
I think the date format is wrong, which one I need to use ?
Thank you!
Update
the problem is that the year day and month is wrong, It should be 14 Dec 2016 instead of Jan 18 1970
Problem is your timeStamp is in seconds , so convert your timeStamp into millisec and then use the date format function ...
try this one ...
JAVA
private String getDate(long time) {
Date date = new Date(time*1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
return sdf.format(date);;
}
Kotlin
fun getDate(time:Long):String {
var date:Date = Date(time*1000L); // *1000 is to convert seconds to milliseconds
var sdf:SimpleDateFormat = SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
return sdf.format(date);
}
Output:- like this
Note:- EEE is represent as Day in Week ,MMM is represent as Month in words and so on ..
You can make any combination if you read this
You need to use EEE, d MMM yyyy
Update
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
sdf.format(new Date(1481709600));
the dateformat is wrong; you have to use the
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(date);
try this
private String getDateFromTimeStamp(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
return date;
}
Try this method to get data. Put the time in setTimeInMillis as long and not as int
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
return date;
}
You can create a simple method to get the date from timestamps as follows
public String getDateCurrentTimeZone(long timestamp) {
try {
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault(); calendar.setTimeInMillis(timestamp * 1000);
calendar.add(Calendar.MILLISECOND,
tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
Date currenTimeZone = (Date) calendar.getTime();
return sdf.format(currenTimeZone);
} catch (Exception e) {
}
return "";
}
This works for me:
val currentDay = Date()
val day = currentDay.time
Logger.i("currentDay = $currentDay and day : $day ")
val c = Calendar.getInstance()
c.timeInMillis = day
val d = c.time
val sdf = SimpleDateFormat("dd/MM/yyyy HH:mm")
Logger.i("meDate : ${sdf.format(d)}")

Invalid Date format for Simplified Chinese

I am getting an Illegal Argument Exception from the Joda time library but only in simplified Chinese.
java.lang.IllegalArgumentException: Invalid format: "10月 01, 2015 10:25 PM"
at org.joda.time.format.DateTimeFormatter.parseDateTime(Unknown Source)
My knowledge of Chinese is zero so I am not sure why it is failing.
You may try like this:
Locale locale = Locale.SIMPLIFIED_CHINESE;
SimpleDateFormat myFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss a",locale);
SimpleDateFormat fromUser = new SimpleDateFormat("dd MMM yyyy HH:mm:ss a",Locale.US);
try {
date = myFormat.format(fromUser.parse(date));
} catch (Exception e) {
// Log.d("tttt","Ex prob ar >"+e.getMessage());
}

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.

SimpleDateFormat parsing troubles

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

Categories

Resources