convert String in time to Time object without Date

前端 未结 3 750
情话喂你
情话喂你 2020-12-06 06:57

i got problem to convert String time to Time object because it print together with Date. this is my code.

String time = \"15:30:18\";

DateFormat sdf = ne         


        
相关标签:
3条回答
  • 2020-12-06 07:15

    Joda-Time | java.time

    If you want a time-only value without a date and without a time zone, then you must use either the Joda-Time library or the new java.time package bundled in Java 8 (inspired by Joda-Time).

    Both of those frameworks offers a LocalTime class.

    In Joda-Time 2.4…

    LocalTime localTime = new LocalTime( "15:30:18" );
    

    In java.time…

    LocalTime localTime = LocalTime.parse( "15:30:18" );
    
    0 讨论(0)
  • 2020-12-06 07:19

    Use the same SimpleDateFormat that you used to parse it:

    String time = "15:30:18";
    
    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    Date date = sdf.parse(time);
    
    System.out.println("Time: " + sdf.format(date));
    

    Remember, the Date object always represents a combined date/time value. It can't properly represent a date-only or time-only value, so you have to use the correct DateFormat to ensure you only "see" the parts that you want.

    0 讨论(0)
  • 2020-12-06 07:25

    This works as well

    String t = "00:00:00" 
    Time.valueOf(t);
    
    0 讨论(0)
提交回复
热议问题