[ACCEPTED]-java get date marker field(am/pm)-java
Accepted answer
You could use a Calendar
.
Calendar cal = Calendar.getInstance();
cal.setTime(st);
if (cal.get(Calendar.AM_PM) == Calendar.PM) {
...
}
Make sure you don't have 1 a time zone mismatch when you do this.
Calendar is probably best, but you could 2 use DateFormat, too:
String amPm = new SimpleDateFormat("aa").format(time);
The TimeZone caveat 1 also applies here.
Please try this
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss_aa");
Calendar cal = Calendar.getInstance();
Log.d("Time", ""+dateFormat.format(cal.getTime()));
int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
System.out.println("time => " + namesOfDays[day-1]+"_"+dateFormat.format(cal.getTime()));
0
use this..
String getTime(String milliseconds) {
String time = "";
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(milliseconds));
time = cal.get(Calendar.HOUR) + " : " + cal.get(Calendar.MINUTE);
if(cal.get(Calendar.AM_PM)==0)
time=time+" AM";
else
time=time+" PM";
return time;
}
0
Calendar
can also do
Calendar cal = Calendar.getInstance();
cal.setTime(st);
String meridiem = cal.getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.getDefault())
meridiem
now equals "PM" based 1 on your code.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.