我想以AM / PM格式显示时间。 示例:上午9:00 我也想进行加法减法运算。我的活动将从上午9:00开始。我想添加分钟来获得结果计划事件。 除了制作自定义Time类之外,我怎么能这样做呢?
从上午9:00开始 加入后加入45分钟 开始时间上午9:45
答案 0 :(得分:10)
从SimpleDateFormat
开始,这将允许您解析和格式化时间值,例如......
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
// Get the start time..
Date start = sdf.parse("09:00 AM");
System.out.println(sdf.format(start));
} catch (ParseException ex) {
ex.printStackTrace();
}
这样,您就可以使用Calendar
来操纵日期值的各个字段......
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, 45);
Date end = cal.getTime();
并将它们放在一起......
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
try {
Date start = sdf.parse("09:00 AM");
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, 45);
Date end = cal.getTime();
System.out.println(sdf.format(start) + " to " + sdf.format(end));
} catch (ParseException ex) {
ex.printStackTrace();
}
输出09:00 AM to 09:45 AM
<强>更新强>
或者您可以使用JodaTime
...
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2).appendLiteral(" ").appendHalfdayOfDayText().toFormatter();
LocalTime start = LocalTime.parse("09:00 am", dtf);
LocalTime end = start.plusMinutes(45);
System.out.println(start.toString("hh:mm a") + " to " + end.toString("hh:mm a"));
或者,如果您正在使用Java 8,那么新的日期/时间API ......
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("hh:mm a").toFormatter();
LocalTime start = LocalTime.of(9, 0);
LocalTime end = start.plusMinutes(45);
System.out.println(dtf.format(start) + " to " + dtf.format(end));
答案 1 :(得分:2)
我想贡献现代的答案
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, "")
.setSmallIcon(R.drawable.my_ic)
.setContentTitle(title)
.setContentText(msg)
.setContentIntent(contentIntent);
输出为:
格式化时间:9:45 AM
在大多数其他答案中使用的 // create a time of day of 09:00
LocalTime start = LocalTime.of(9, 0);
// add 45 minutes
start = start.plusMinutes(45);
// Display in 12 hour clock with AM or PM
DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(Locale.US);
String displayTime = start.format(timeFormatter);
System.out.println("Formatted time: " + displayTime);
,SimpleDateFormat
和Date
类不仅设计欠佳(尤其是第一个特别麻烦的地方),而且自Java以来它们也已经过时了四年前问这个问题时,现代Java日期和时间API .time已经不存在了。
为了向用户显示一段时间,我通常建议您从Calendar
,DateTimeFormatter.ofLocalizedDate
和.ofLocalizedTime
获得的内置格式。如果您在某些情况下有内置格式无法满足的特殊格式需求,则也可以指定自己的格式,例如:
.ofLocalizedDateTime
(此特定示例没有意义,因为它给出的结果与上述相同,但是您可以将其用作起点并根据需要进行修改。)
链接: Oracle tutorial: Date Time解释了如何使用 DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.US);
。
答案 2 :(得分:0)
取自http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
"h:mm a" gives 12:08 PM
按时执行添加使用Calendar类
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#add(int,%20int)
Calendar rightNow = Calendar.getInstance(); // or use your own Date
rightNow.add (Calendar.MINUTE, 45);
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
System.out.println(dateFormat.format (rightNow)); --> showing as am / pm
答案 3 :(得分:0)
找到许多例如here
的例子import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
String strDateFormat = "HH:mm:ss a";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println(sdf.format(date));
}
}
//10:20:12 AM
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
阅读this
答案 4 :(得分:0)
使用Calendar
Calendar calendar =Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
sdf.format(calendar.getTime());
System.out.println(sdf.format(calendar.getTime()));
// i want to add 45mins now
calendar.add(Calendar.MINUTE,45);
System.out.println(sdf.format(calendar.getTime()));
// i want to substract 30mins now
calendar.add(Calendar.MINUTE,-30);
System.out.println(sdf.format(calendar.getTime()));
Out put:
10:49 AM
11:34 AM
11:04 AM
答案 5 :(得分:0)
使用Simpledatetimeformat对象格式化时间和日历对象与日期以在日期对象上添加时间日期
答案 6 :(得分:0)
使用日期模式获取它的最简单方法 - h:mm a,其中
h - Hour in am/pm (1-12)
m - Minute in hour
a - Am/pm marker
Code snippet :
DateFormat dateFormat = new SimpleDateFormat(“hh:mm a”);
答案 7 :(得分:0)
Calendar cl = new GregorianCalendar();
int a = cl.get(Calendar.AM_PM);
if(a == 1) {
lbltimePeriod.setText("PM");
}
else
{
lbltimePeriod.setText("AM");
}
这肯定会解决你的问题,它对我有效100%
答案 8 :(得分:0)
edit_event_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar =Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
String time = sdf.format(calendar.getTime());
Log.e("time","time "+sdf.format(calendar.getTime()));
String inputTime = time, inputHours, inputMinutes;
inputHours = inputTime.substring(0, 2);
inputMinutes = inputTime.substring(3, 5);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
if (selectedHour == 0) {
selectedHour += 12;
timeFormat = "AM";
} else if (selectedHour == 12) {
timeFormat = "PM";
} else if (selectedHour > 12) {
selectedHour -= 12;
timeFormat = "PM";
} else {
timeFormat = "AM";
}
String selectedTime = selectedHour + ":" + selectedMinute + " " + timeFormat;
edit_event_time.setText(selectedTime);
}
}, Integer.parseInt(inputHours), Integer.parseInt(inputMinutes), false);//mention true for 24 hour's time format
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
答案 9 :(得分:-1)
有一个简单的代码来生成AM / PH的时间这里是我给你的代码请检查这个
import java.text.SimpleDateFormat; import java.util.Date;
public class AddAMPMToFormattedDate {
public static void main(String [] args){
//create Date object
Date date = new Date();
//formatting time to have AM/PM text using 'a' format
String strDateFormat = "HH:mm:ss a";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("Time with AM/PM field : " + sdf.format(date));
} }