Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyway of getting am/pm from time? [duplicate]

Tags:

android

Possible Duplicate:
Displaying the Time in AM/PM format in android

I can get the time and turn it into string using:

public static final String TIME_FORMAT = "h:mm a";
       SimpleDateFormat dateTimeFormat = new SimpleDateFormat(TIME_FORMAT);

String getstring = dateTimeFormat.format(when.gettime();

but how do i get am/pm from it and turn it into string beccause i need it?

like image 321
Michael Avatar asked Jul 12 '12 01:07

Michael


People also ask

How do I display AM PM in date format?

There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.


2 Answers

try this

Calendar now = Calendar.getInstance();
int a = now.get(Calendar.AM_PM);
if(a == Calendar.AM)
   System.out.println("AM"+now.get(Calendar.HOUR));
like image 60
nyjsl Avatar answered Oct 06 '22 00:10

nyjsl


This should work:

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
 new TimePickerDialog.OnTimeSetListener()

{

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView datehid = (TextView)findViewById(R.id.timehidden);
if(hourOfDay>12)
{

    datehid.setText(String.valueOf(hourOfDay-12)+ ":"+(String.valueOf(minute)+"pm"));
}
if(hourOfDay==12)
{
    datehid.setText("12"+ ":"+(String.valueOf(minute)+"pm"));
}
if(hourOfDay<12)
{
    datehid.setText(String.valueOf(hourOfDay)+ ":"+(String.valueOf(minute)+"am"));
}
}
};

Found on an earlier question.

like image 40
Nathan Avatar answered Oct 06 '22 00:10

Nathan