Working on Contact birthday and Anniversary:
I get details and birthday like this 12.2.2012 or 12/2/2012 or 12-02-2012 or 2/12/12
Question:
Is the date Format same across all Samsung Phones. IF yes what is the date format.
(Guess won't work on all Android phones as birthday dates are stored in many different format)
How to identify the date format like if the date is 12.2.2012 or Feb 12 2012 or any other date string pattern. Is it of format "yyyy-MM-dd" or "MMM dd, yyyy" or any other?
ex: if date is "Feb 12 2012" then date format is "MMM dd yyyy"
Unfortunately it seems different apps/vendors use different formats. My solution for this is to try parsing different formats until i either find the right one or give up.
Here is some sample code:
public static final SimpleDateFormat[] birthdayFormats = {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyyMMdd"),
new SimpleDateFormat("yyyy.MM.dd"),
new SimpleDateFormat("yy-MM-dd"),,
new SimpleDateFormat("yyMMdd"),
new SimpleDateFormat("yy.MM.dd")
new SimpleDateFormat("yy/MM/dd"),
new SimpleDateFormat("MM-dd"),
new SimpleDateFormat("MMdd"),
new SimpleDateFormat("MM/dd"),
new SimpleDateFormat("MM.dd"),
};
.....
Date birthday = null;
for (SimpleDateFormat f : birthdayFormats) {
try {
birthday = f.parse(birthdaystr);
if (birthday!=null) {
contact.setBirthday(birthday);
break;
}
} catch (Exception e) {
continue;
}
}
Use DateFormat.getDateInstance(int style, Locale locale) instead of creating your own patterns with SimpleDateFormat.
Another way that u want to get date in String and after pass in below code
String dateStr = "04/05/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
The date is stored as "YYYY-MM-DD".
Use the date formater class to convert it into any format you need.
When you want to update put it in the same format as you have read.
Related
I want to get date and time from 2019-06-27T12:30:00.000+0000 in android. I tried a code but it is not working.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = null;//You will get date object relative to server/client
timezone wherever it is parsed
try {
date = dateFormat.parse("2019-06-27T12:30:00.000+0000");
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
String dateStr = formatter.format(date);
You're missing out on parts of the Date
The pattern you need is,
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'ssss");
You're missing the millisecond portion of your SimpleDateFormat, below is what you need for that specific pattern:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
I am getting Time from an api in this format
"2018-03-07T11:19:54.686Z" ..... How to convert this String data to mm-dd-yyyy date format in android.
SimpleDateFormat format = new SimpleDateFormat(" MM,dd,yy");
String date = format.format(Date.parse(data));
I have tried using the above two but there is a parse error.
First you have to parse the given date "2018-03-07T11:19:54.686Z" in the same format and then you can apply formatting with the desired format.
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String date = format.format(parseDateFormat.parse(data));
you used below code and show your format according to date.
String date="2018-03-07T11:19:54.686Z";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateFormat formatdate = new SimpleDateFormat("MM,dd,yy");//if show mm-dd-yy
try {
Date date1=parseDateFormat.parse(date);
Log.d("New Date",formatdate.format(date1));
} catch (ParseException e) {
e.printStackTrace();
}
i would like to set the date format of my date picker like this:
yyyy-MM-dd HH:ss:mm
I hope this is the correct format for dates in databases. if not, please tell me which format is right.
also i would like to set the hour,minute and second of selected date like 0
Example:
2015-11-30 00:00:00
For this i use this code:
long dateTime = DatePicker.getCalendarView().getDate();
Date date = new Date(dateTime);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Android Studio tells me, that setHours,setMinutes and setSeconds is deprecated. what is the new way to set this values?
Use Calendar Object instead of Date:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(DatePicker.getCalendarView().getDate(););
c.set(Calendar.HOUR,00);
like wise
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
String dateString = "2015-11-30 00:00:00";
try {
Date date = df.parse(dateString);
df.applyPattern("yyyy-mm-dd hh:mm:ss");
String result = df.format(date);
}
catch (ParseException e){
e.printStackTrace();
}
I have a string that contains a date like so:
String startTime = "2014-10-11T17:00:41+0000"
I am trying to reformat that string so that it reads like so instead:
Oct 11, 2014 5:00 PM
Since Date objects do not keep time zone information, you need to specifically set the time zone offset of original date to the target formatter. Here is the complete code for transforming from one format to another while maintaining the time zone offset (+0000 in your case). More information on TimeZone class here and on how to build a proper date and time pattern string for your requirement here.
try {
DateFormat originalFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat(
"MMM dd, yyyy K:mm a", Locale.ENGLISH);
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Date date = originalFormat.parse("2014-10-11T17:00:41+0000");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: Oct 11, 2014 5:00 PM
Use SimpleDateFormat for parse input string and represent in new format:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ex.:
SimpleDateFormat sdfmtIn = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat sdfmtOut= new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date date = sdfmtIn.parse(strInput);
String strOutput = sdfmtOut.format(date);
I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...