简体   繁体   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";

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 如果您使用的是

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.

相关问题 如何将 2017-08-01T16:00:00-04:00 转换为 Java 中的时间戳? - How to convert 2017-08-01T16:00:00-04:00 to timestamp in Java? 无法解析的日期:“2011-12-08T02:01:02+01:00” - Unparseable date: “2011-12-08T02:01:02+01:00” Android:如何以以下格式“ 2015-10-08T08:09:22.067 + 00:00”立即获取日期时间字符串? - Android : how to get now date time string in following format “2015-10-08T08:09:22.067+00:00”? 如何格式化此日期“ 2018-08-02T00:00:00 + 05:30”字符串为此“ 2018-18-02” - How to Format this Date “2018-08-02T00:00:00+05:30” String to this “2018-18-02” DateTimeParseException:无法解析文本“2020-04-01T08:53:47.000+02:00 00:00”,在索引 29 处找到未解析的文本 - DateTimeParseException: Text '2020-04-01T08:53:47.000+02:00 00:00' could not be parsed, unparsed text found at index 29 在Android中将字符串转换为日期“ 2016-01-28T12:08:47.676706-05:00” - Convert String to Date “2016-01-28T12:08:47.676706-05:00” in android 如何获取Spring Boot API返回MySQL日期值的UTC时间而不是“ 2018-08-01T04:00:00Z”? - How do I get my Spring Boot API to return UTC times for MySQL Date values instead of “2018-08-01T04:00:00Z”? java.text.ParseException:无法解析的日期:“ 2015-08-19T00:00:00” - java.text.ParseException: Unparseable date: “2015-08-19T00:00:00” 将字符串解析为时间使得01:00:00 - Parsing String to Time makes 01:00:00 java.text.ParseException:无法解析的日期:“ 2012年1月11日08:00:00” - java.text.ParseException: Unparseable date:“01/11/2012 08:00:00”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM