date format has this am_pm field but how can put conditions in an if statement?
if(time = "pm") {
*do this* }
something like that. Is that possible?
Thanks for the help guys :)
AM_PM_FIELD is a constant int. I assume you want to see if a time is AM or PM? Check out the SimpleDateFormat, http://developer.android.com/reference/java/text/SimpleDateFormat.html
I haven't tested the following code so don't quote me
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("a");
String amOrPm = df.format(c.getTime());
Related
I want show time in TextView using below code.
PersianCalendar persianCalendar = new PersianCalendar();
persianCalendar.setTime(mDate);
Calendar cal = Calendar.getInstance();
cal.setTime(mDate);
holder.tvTime.setText(cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
Problem occurs when time is for example 12:03 in TextView displayed as 12:3
How can I fix this?
You can format a Date to a String like this (using java.text.SimpleDateFormat)
holder.tvTime.setText(new SimpleDateFormat("H:mm").format(mDate));
The H format specifier means Hour in day (0-23), and the mm means Minute in hour with two digits.
See the documentation for other formatting options.
Use this code to format minute in 2 digits..
holder.tvTime.setText(String.format("%d:%02d",cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
Edit - As suggested by #henry.. you can directly go with this.. holder.tvTime.setText(String.format("%d:%02d",cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
I had same issue and solved by this.
Probably it is not the best solution, but it worked for me.
PersianCalendar persianCalendar = new PersianCalendar();
persianCalendar.setTime(mDate);
Calendar cal = Calendar.getInstance();
cal.setTime(mDate);
String minuteSelected = String.valueOf(cal.get(Calendar.MINUTE));
String minutWith0 = ((minuteSelected.length() == 1 ? "0" + minuteSelected : minuteSelected));// to get minut "01" insted of "1"
holder.tvTime.setText(cal.get(Calendar.HOUR_OF_DAY)+":"+ minutWith0);
I create a date and then format is like this:
Example 1:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String currentDate = sdf.format(new Date());
What I would like to do is check if this date is before another date (also formatted the same way). How would I go about doing this?
Example 2:
Also, how would I check whether one of these is before another:
long setForLong = System.currentTimeMillis() + (totalTime*1000);
String display = (String) DateFormat.format("HH:mm:ss dd/MM/yyyy", setForLong);
EDIT:
I think more detail is needed. I create a date in two different ways for two different uses. The first use just formats the current date into a string so it is readable for the user. In the second case, I am using a date in the future with System.currentTimeMillis and adding on a long. Both result in a string.
Both methods format the date in exactly the same way, and I set the strings into a TextView. Later, I need to compare these dates. I do not have the original data/date/etc, only these strings. Becasue they are formatted in the same way, I though it would be easy to compare them.
I have tried the if(String1.compareTo(String2) >0 ) method, but that does not work if the day is changed.
If you only have two String objects that are dates available to you. You will need to process them in something, either in your own comparator class or in another object. In this case, since these are already formatted into dates, you can just create Date objects and compare using the methods previously posted. Something like this:
String string = "05:30:33 15/02/1985";
Date date1 = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.ENGLISH).parse(string);
String string2 = "15:30:33 01/02/1985";
Date date2 = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.ENGLISH).parse(string2);
if(date1.getTime()>date2.getTime()) {
//date1 greater than date2
}
else if(date1.getTime()<date2.getTime()) {
//date1 less than date2
}
else {
//date1 equal to date2
}
You should use Calendar for convenient comparing dates.
Calendar c1 = Calendar.getInstance();
c1.setTime(Date someDate);
Calendar c2 = Calendar.getInstance();
c2.setTime(Date anotherDate);
if(c1.before(c2)){
// do something
}
And you can format it at any time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String currentDate = sdf.format(c1.getTime());
Does anyone here know what the best way is to calculate the date 2 days in the past?
I've got this piece of code to retrieve the current date:
public static String getDateTime (String Format){
SimpleDateFormat sdf = new SimpleDateFormat(Format);
return sdf.format(new Date());
}
But I want to be able to calculate the date 2 days in the past. So decrease the date with 2 days. Anyone who knows what the best way is to do this?
Thanks in advance
Using Calendar is probably the easiest way. Assuming that you have defined Format as per the question:
// get Now
Calendar cal = Calendar.getInstance();
// go back two days
cal.add(Calendar.DAY_OF_YEAR, -2);
// display
SimpleDateFormat sdf = new SimpleDateFormat(Format);
String string = sdf.format(cal.getTime());
Just use Calendar's add() function:
Calendar c = Calendar.getInstance();
c.setTime(yourDateObject);
c.add(Calendar.DAY_OF_MONTH, -2);
It will automatically change the month, year, etc. if necessary.
Im working on android and parse some XML file
I get some date with this
DateFormat h = new SimpleDateFormat ("hh:mm:ss", Locale.FRANCE);
DateFormat d = new SimpleDateFormat ("yyyy-M-dd'T'hh:mm:ss", Locale.FRANCE);
Date hdebut = h.parse(maString);
Probleme is Date is for Date not hour so it give me a 1 january 1970(start of timestamp right?) a the correct our so i can't compare by using
Date now new Date();
now.after(hdebut);
i have some method to getHours or month but they are decrepetead so i don't know if i can use them or if wa have a better way no to do it
Any idea?
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
Try this. There are a lot of constants you can use with Calendar class.
Date is deprecated. You should be using Calendar instead.
Calendar provides working after and before methods as well and should work with pretty much any date you'll get to use :).
Hi i want to Get System Date In User Friendly Manner Like February 14, 2011 12:00 AM. How can I do this in android?
Here you can use SimpleDateFormat class in which you can get Date and time as you want. You can write something like this
Configuration mConfiguration = new Configuration();
Settings.System.getConfiguration(getContentResolver(), mConfiguration);
final TimeZone mTimeZone = Calendar.getInstance(mConfiguration.locale).getTimeZone();
SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("MM.dd HH:mm");
mSimpleDateFormat.setTimeZone(mTimeZone);
mRefreshDateTime = mSimpleDateFormat.format(new Date());
Where in SimpleDateFormat you can pass as you like. You can find the class parameters from here: SimpleDateFormat
I hope it would help you.
Calendar mReminderCal = Calendar.getInstance();
mReminderCal.setTimeInMillis(System.currentTimeMillis());
Date dateText = new Date(mReminderCal.get(Calendar.YEAR)-1900,
mReminderCal.get(Calendar.MONTH),
mReminderCal.get(Calendar.DAY_OF_MONTH),
mReminderCal.get(Calendar.HOUR_OF_DAY),
mReminderCal.get(Calendar.MINUTE));
String dateString = android.text.format.DateFormat.format("MM/dd/yyyy hh:mm", dateText));
Use SimpleDateFormat.
SimpleDateFormat like in Java SE