我正在尝试向android中的当前时间添加分钟数。
我想将Simpledateformat用作(“ EEE,d MMM yyyy HH:mm:ss Z”)
首先,我想解释一下我的概念。
我已经使用了代码...
String t = time.getText().toString();
int ti = Integer.parseInt(t);
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Calendar now = Calendar.getInstance();
String a = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
now.add(Calendar.MINUTE,ti);
a = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
adminList = mFirestore.collection("AdminList");
Map<String,Object> k = new HashMap<>();
k.put("order",order);
k.put("status","Pending");
k.put("hotel_name",hotel);
k.put("user_id",userid);
k.put("time",a);
adminList.document(order).update(k);
此代码是
void updateTimeRemaining(String text) {
if (o.getOrder().equalsIgnoreCase("order1")) {
try {
TimeZone.setDefault(TimeZone.getDefault());
String stop = ol.getTime();
@SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Calendar end_date = Calendar.getInstance();
end_date.setTime(format.parse(stop));
final Calendar today = Calendar.getInstance();
timeDiff = end_date.getTimeInMillis() - today.getTimeInMillis();
if (timeDiff > 0) {
long seconds = timeDiff / 1000 % 60;
long minutes = timeDiff / (60 * 1000) % 60;
long hours = timeDiff / (60 * 60 * 1000) % 24;
//long days = (int) timeDiff / (24 * 60 * 60 * 1000);
//long days = TimeUnit.MILLISECONDS.toDays(timeDiff);
String left = "";
//if (days > 0)
// left += days + " " + /*context.getString(R.string.txt_day) +*/ ":";
if (hours > 0)
left += hours + " " +/* context.getString(R.string.txt_hour) + */":";
if (minutes > 0)
left += minutes + " " +/* context.getString(R.string.txt_minute) + */":";
left += seconds + " " /*+ context.getString(R.string.txt_second)*/;
String finalLeft = left;
text = finalLeft;
time.setText("00:" + finalLeft);
} else {
time.setText("Finished");
}
// }
} catch (Exception ex) {
ex.printStackTrace();
}
// }
}
所以我的最终问题是,
如何向当前时间添加50分钟,格式为“ EEE,d MMM yyyy HH:mm:ss Z”
答案 0 :(得分:2)
减轻您的头痛,并使用Jodatime或ThreeTenABP之类的库。
您的代码可以很简单:
DateTimeFormatter dateFormatter = DateTimeFormat
.forPattern("EEE, d MMM yyyy HH:mm:ss Z");
LocalTime now = new LocalTime();
LocalTime then = now.plusMinutes(50);
Log.d(TAG, dateFormatter.print(then));
在Java 8以下管理日期和时间是一个不可读,不可靠的混乱;您现在可能已经意识到了。