Anyway, get it from time to time?

Possible duplicate:
Android AM / PM time display

I can get the time and turn it into a 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 a beccause string, do I need it?

+6
source share
3 answers

try it

 Calendar now = Calendar.getInstance(); int a = now.get(Calendar.AM_PM); if(a == Calendar.AM) System.out.println("AM"+now.get(Calendar.HOUR)); 
+9
source

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 .

+2
source

From your code, it looks like you are using Date objects. Instead, I recommend using calendar objects, if possible. If necessary, you can get the Date object from the Calendar object using the Calendar getTime() method.

If you have a Calendar object, you can do the following:

 Calendar cal = ...; String ampm = DateUtils.getAMPMString(cal.get(Calendar.AM_PM)); 

DateUtils is part of the Android platform and returns back to API level 3. getAMPMString() returns a localized version of AM / PM. See docs on the Android developer site.

If you do not have a Calendar object available, you can create it from a date object:

 Date date = ...; Calendar cal = Calendar.getInstance(); cal.setTime(date); 

However, since the calendar is on the hard side, you are better off working with calendar objects directly, rather than creating and spoofing them.

+1
source

Source: https://habr.com/ru/post/920278/


All Articles