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();
}
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 was having difficult to put correct types for:
private static final Date myDate =
I tried to put "mm-dd-yyyy" but it said it cannot be a string.
I tried 'mm-dd-yyyy' but it said too many characters in character literal.
I dont know what is other types I should put. Help please.
If you want to have a Date with a specific time you can use a Calendar object.
If you want to have todays Date you can just use:
private static final Date myDate = new Date();
Specific date:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date myDate = cal.getTime();
That would be 01.01.2015
Try with this:
SimpleDateFormat f = new SimpleDateFormat("mm-dd-yyyy");
Date date = f.parse(yourString);
I expect it will works for you!
EDIT: It gives to you an error because you are trying set to your variable myDate a String, and it's a type of Date.
Try something like:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
...as you can see here:
http://www.mkyong.com/java/how-to-convert-string-to-date-java/
Is it possible to store the current date in a file but no as a string?
What I mean:
If I use :
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
date.add(formattedDate);
It stores (with the format I want) in date list (which is a String List).
I want 'date' to be a Date List and store it to a file.
Is it possible to store it as dd/MM/yyyy ?Because like this:
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
date.add(d);
it stores as
Thu Apr 18 17:06:14 GMT+03:00 2013
Try the above code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
dateFormat.format(date);
This will not save the date as string(as you desire) and will format the current date according to your need. I haven't tested it right now, but as I have used this before so I am pretty sure this works. Hope this helps. If it does not then please comment.
Update
I tried following:
List<Date> d = new ArrayList<Date>();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String s = dateFormat.format(date);
try {
d.add(dateFormat.parse(s));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.d("try","NewDateString: "+ d.get(0));
Log.d("try","NewDate: "+ d.get(0).getDate()+" "+d.get(0).getMonth()+" "+(d.get(0).getYear()+1900));
It works and following was the result I got:
NewDateString: Thu Apr 18 00:00:00 EDT 2013
New Date: 18 3 2013
So what you can do is, save the date as above and when you are retrieving the date from the file you may choose to get the Day/Month/Year from the date object that you saved.
I am trying to get long value of a string (date & time string), but it is not working. What I am trying to do is:
Choose date form datepicker and store it in a String
Choose time from timepicker and store it in a String
Then I concatenate these two strings and get long value from that string.
I have tried a few Date formatters but I am unable to get this done. The format of my string is dd-MM-yyy h:mm a. Please help me out of this. Provide any utility available for this.
Try this:-
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyy h:mm a");
Date myDate = new Date(); // Default Value.
try {
myDate = sdf.parse(dateString);
} catch (ParseException e) {
// Do Something on Error.
}
Long dateTimeinLong = myDate.getTime();
where dateString is your concatenated String of date and time.
Forget about strings, and go directly with the values:
DatePicker dp = (DatePicker) findViewById...
TimePicker tp = (TimePicker) findViewById...
Date timeStamp = new Date( dp.getYear(), dp.getMonth(), dp.getDay(), tp.getHour(), tp.getMinute(), 0 );
long longTime = timeStamp.getTime();
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.