繁体   English   中英

如何获取此字符串的 08:00 2015-01-01T08:00:00-02:00

[英]How to get 08:00 of this String 2015-01-01T08:00:00-02:00

 String date = "2015-01-01T08:00:00-02:00";

我只需要得到 08:00

你能帮忙解析这个格式吗?

我在下面尝试了这段代码。 但不工作

 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;
    }
}

如果您使用的是

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

您需要将其转换为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);

使用此日期在日历中设置它。 您可以自行使用日期对象,但它已弃用https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

我找到了解决方案

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM