I am working on Android application in which I am going to send my date to the server. But I am getting the error of "Unparsable date exception", my date is: "3/12/2018".
My code is given below:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Date parsedDate = sdf.parse(setDateTxt); //setDateTxt returns 3/12/2018 from date picker
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
object.put("dateOfBirth", print.format(parsedDate));
the SimpleDateFormat has to match your setDateText pattern.
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy",Locale.ENGLISH);
Related
I have a date instance, how to convert it into local time zone?
Date meetingStartDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.StartDateTimeUtc);
You can use below code to get time in local time zone
Date meetingStartDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.StartDateTimeUtc);
DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
formatter.setTimeZone(TimeZone.getDefault());
System.out.println(formatter.format(meetingStartDate));
SimpleDateFormat df = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH);
df.setTimeZone(TimeZone.getDefault());
df = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH);
df.setTimeZone(TimeZone.getDefault());
date == null ? dateStr : df.format(meetingStartDate );
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.
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());
}
I tried the following code for this.
private SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMM");
dateFormatter.parse("13 Jan");
but it raising parse exception invalid date.
This is the dd MMM format. So you need to use it as
new SimpleDateFormat("dd MMM");
Read Date and Time Patterns documentation for more details.
There might another issue. For your current input, format "d MMM" will also work.
Try it as:
final Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM"); // dd for date, MMM for Month
Toast.makeText(getApplicationContext(), formatter.format(calendar.getTime()), Toast.LENGTH_SHORT).show();
Hope it helps.
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.