[英]How to get 08:00 of this String 2015-01-01T08:00:00-02:00
String date = "2015-01-01T08:00:00-02:00";
and I need get only 08:00我只需要得到 08:00
Can you please help to parser this formate?你能帮忙解析这个格式吗?
I tried this code below.我在下面尝试了这段代码。 but not work
但不工作
try {
String[] buffer = null;
buffer = dateString.split("T");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.parse(buffer[0]);
} catch (ParseException e) {
Log.e("Error", e.getMessage());
return null;
}
}
If you're using java-8如果您使用的是java-8
String date = "2015-01-01T08:00:00-02:00";
ZonedDateTime ldt = ZonedDateTime.parse(date);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
System.out.println(dtf.format(ldt)); // 08:00
// Thank @MadProgrammer for the following ones. :)
System.out.println(ldt.toLocalTime()); // Also prints 08:00
System.out.println(ldt.getHour()+":"+ldg.getMinute()); // Prints 8:0 but you could use
// those variable for other things
// if you do not need to print them
You need to Convert this to Calendar object and then you can get what ever you want from it.您需要将其转换为Calendar对象,然后您就可以从中获得您想要的任何内容。
String dateInString = new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
.format(cal.getTime())
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss");
Date parsedDate = formatter.parse(dateInString);
Use this date to set it in Calendar.使用此日期在日历中设置它。 You could use the date object it self but it deprecated https://docs.oracle.com/javase/7/docs/api/java/util/Date.html .
您可以自行使用日期对象,但它已弃用https://docs.oracle.com/javase/7/docs/api/java/util/Date.html 。
I found the solution我找到了解决方案
String date = "2015-01-01T08:00:00-02:00";
try{
String[] buffer = null;
buffer = horaInicio.split("T");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.parse(buffer[1]);
}catch (Exception e) {
Log.e("Error", e.getMessage());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.