I am using a TimePicker in my app. Whenever the user opens the screen with the TimePicker, I initialize it with the current time. When I do this, the TimePicker shows AM instead of PM. Why does this happen? Is there anything I have to add in my code?
Date initDate = m_NoteManageObject.getDateTimeArray(position);
Calendar myCal = Calendar.getInstance();
myCal.setTime(initDate);
TimePicker timePicker = (TimePicker)layout.findViewById(R.id.time_picker);
// Initialise Time Picker
timePicker.setCurrentHour(myCal.get(Calendar.HOUR));
timePicker.setCurrentMinute(myCal.get(Calendar.MINUTE));
You'll have to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR.
i.e., timePicker.setCurrentHour() always expects the argument in 24-hour format. Unfortunately, this fact is not documented properly in the API documentation.
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute) {
if(hourOfDay>=0 && hourOfDay<12){
time = hourOfDay + " : " + minute + " AM";
} else {
if(hourOfDay == 12){
time = hourOfDay + " : " + minute + "PM";
} else{
hourOfDay = hourOfDay -12;
time = hourOfDay + " : " + minute + "PM";
}
}
deliveryTime.setText(time);
}
I think you have to override the onTimeSet() method of the Timepicker. Check this link
TimePickerDialog and AM or PM
May it helps you..
First, get an instance of the Calendar class, then use HOUR_OF_DAY to get the hour. HOUR_OF_DAY will be in 24-hour format so just assign that to a variable and then check whether that int is greater than 0 and less than 12. If this condition is true then append AM or else PM. Because HOUR_OF_DAY is 24-hour format, the PM hours will be displayed as 13,14 so to handle this, just subtract the HOUR_OF_DAY by 12. Here is the code:
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour < 12 && hour >= 0) {
tv.setText(hour + " AM");
} else {
hour -= 12;
if(hour == 0) {
hour = 12;
}
tv.setText(hour + " PM");
}
Try this :
Calendar calendar = Calendar.getInstance();
calendar.set(0, 0, 0, hourOfDay, minute, 0);
long timeInMillis = calendar.getTimeInMillis();
java.text.DateFormat dateFormatter = new SimpleDateFormat("hh:mm a");
Date date = new Date();
date.setTime(timeInMillis);
time.setText(dateFormatter.format(date));
Related
Time class is no longer possible to use.
I want to ask you, how to detect in app 3-4am?
I need that to set up for example night mode in my app.
Can you give me some example how to do it?
Instead of using Time (because Time class was deprecated in API level 22.) you can use Calendar for getting current hour
val rightNow = Calendar.getInstance()
val currentHourIn24Format: Int =rightNow.get(Calendar.HOUR_OF_DAY) // return the hour in 24 hrs format (ranging from 0-23)
val currentHourIn12Format: Int = rightNow.get(Calendar.HOUR) // return the hour in 12 hrs format (ranging from 0-11)
We can use the Calendar class to get a format like "HH:mm:ss"
Calendar calendar = Calendar.getInstance();
int hour24hrs = calendar.get(Calendar.HOUR_OF_DAY);
int hour12hrs = calendar.get(Calendar.HOUR);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
System.out.println("Current hour 24hrs format: " + hour24hrs + ":" + minutes +":"+ seconds);
System.out.println("Current hour 12hrs format: " + hour12hrs + ":" + minutes +":"+ seconds);
Other option using the Date class and applying the format "HH:mm:ss":
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String dateformatted = dateFormat.format(date);
System.out.println(dateformatted);
You can use following methods:
SimpleDateFormat format = new SimpleDateFormat("HH", Locale.US);
String hour = format.format(new Date());
Calendar calendar = Calendar.getInstance();
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
How do you format correctly according to the device configuration a date and time when having year, month, day, hour and minute? for example I want to display 29 July, 2015, 10:30 Am, according to my time zone
You can use this method to format the datetime... u can replace new java.util.Date() with any datetime variable...
android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("dd-MMM-yyyy hh:mm aa", new java.util.Date());
String strDateTime = "29 July, 2015, 10:30 Am";
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM, yyyy, hh:mm a");
strDateTime = sdf.format(Calendar.getInstance().getTime());
holder.txtTime.setText(strDateTime);
Please integrate the above code, that works fine for me.
Any help, do let me know.
Please Find the below solution
Calendar ci = Calendar.getInstance();
String AM_PM;
if(ci.get(Calendar.AM_PM)==0)
{
AM_PM ="AM";
}
else
{
AM_PM ="PM";
}
String CiDateTime = "" + ci.get(Calendar.DAY_OF_MONTH)+" "+
(ci.get(Calendar.MONTH) + 1)+","+
ci.get(Calendar.YEAR) + "-" +
+ "," +getCurrentTime()
+" "+AM_PM;
Call the method
private String getCurrentTime()
{
int hrsRight = 0;
Calendar c = Calendar.getInstance();
int hrs = c.get(Calendar.HOUR);
int min = c.get(Calendar.MINUTE);
if (hrs>12)
{
hrsRight = hrs - 12;
}
else
{
hrsRight = hrs;
}
return String.valueOf(hrsRight)+":"+String.valueOf(min);
}
I have TimePicker in my Registration class to ask the user for their free time each day but it display the time in 24 hour format and I don't know how to view the time in a 12 hour format
I'm getting the value of TimePicker using
int hour = timepicker.getCurrentHour();
int min = timepicker.getCurrentMinute();
String time = hour + ":" + min;
and I also want to get the value of AM/PM
int hour = timepicker.getCurrentHour();
int min = timepicker.getCurrentMinute();
String state = "am";
if(hour > 12){
hour -=12;
state = "pm";
}
String time = hour + ":" + min + ":" + state;
PS: if you want the view itself show the time in 12-hours format you can add timepicker.setIs24HourView(false);
I know a lot of tutorials there about calendar giving minute and hours without leading zero. But those doesnt work for my codes. Can someone help me how to do this base on my codes? Calendar.MINUTE is not showing the zero integer whenever the minite is from 0-9.
Calendar cal = GregorianCalendar.getInstance();
String am_pm, hour;
cal.add(Calendar.MONTH, +1);
if(cal.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
tv.setText(cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.YEAR));
tv2.setText(cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) + " " + am_pm);
tv3.setText("Offered");
you can use DateFormat or Simple way to do this is
String month=String.format("%02d",cal.get(Calendar.MONTH));
String day=String.format("%02d",cal.get(Calendar.DAY_OF_MONTH));
String year=String.format("%02d",cal.get(Calendar.YEAR));
tv.setText( month+ "-" + day + "-" +year );
Use DateFormat for text output.
For example:
Calendar cal = GregorianCalendar.getInstance();
String am_pm, hour;
cal.add(Calendar.MONTH, +1);
Date date = cal.getTime();
DateFormat[] formats = new DateFormat[] {
DateFormat.getDateInstance(),
DateFormat.getDateTimeInstance(),
DateFormat.getTimeInstance(),
};
for (DateFormat df : formats) {
System.out.println(df.format(date));
}
I've been trying this from a long time.
I'm having today's date and I want to compare it with specific date.
But when I set particular date like this, it gives me wrong output:
Calendar cal;
SimpleDateFormat dateformatter;
String currentdate;
cal = Calendar.getInstance();
dateformatter = new SimpleDateFormat("yyyy-MM-dd");
//Getting today's date
currentdate = dateformatter.format(cal.getTime());
Log.i(Class_Tag, "current = " + currentdate);
//Setting specific date
Date start = cal.getTime();
cal.set(2012, 06, 20);
Date end = cal.getTime();
Log.i(Class_Tag, "end = " + dateformatter.format(end));
long diff = end.getDate() - start.getDate();
Log.i(Class_Tag, "diff : " + diff);
long diff1 = end.getMonth() - start.getMonth();
Log.i(Class_Tag, "diff mnth : " + diff1);
long diff2 = end.getYear() - start.getYear();
Log.i(Class_Tag, "diff yr : " + diff2);
if (start.compareTo(end) == 0) {
Log.i(Class_Tag, "EQUAL");
} else if (start.compareTo(end) < 0) {
Log.i(Class_Tag, "end b4 start, delete");
} else {
Log.i(Class_Tag, "start b4 end, do nothing");
OUTPUT:
current = 2012-06-20
end = 2012-07-20 ////WHY it is showing month as '7' when I've set it to '6'?
diff : 0
diff mnth : 1 //Because of the wrong month in end date, this comes 1 instead of 0
diff yr : 0
end b4 start, delete //this also is not correct
I've tried several solutions, but no gains.
My only objective is to get whether that particular date has passed today's date or not.
Any help appreciated.
Edit
in the following code snippet,
//Setting specific date
Date start = cal.getTime();
cal.set(2012, 06, 20);
Date end = cal.getTime();
If I set cal.set(2012, 06, 20); to cal.set(2012, 05, 20);, the month diff. comes out to 0.
This implies that month in particular date is getting incremented by '1', but WHY?
Try this;
Calendar cal = Calendar.getInstance();
long dateNowMillis = cal.getTimeInMillis();
cal.set(2013, 1, 23);
long specDate = cal.getTimeInMillis();
if(dateNowMillis > specDate)
{
System.out.println("Passed");
}
else if(dateNowMillis == specDate)
{
System.out.println("Now");
}
else
{
System.out.println("Not passed");
}
FYI,
A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
You can check read more about Date here and more about SimpleDateFormat.
Update:
One more thing about getMonth() => Its deprecated, instead Calendar.get(Calendar.MONTH), check here.