Adding a leading zero to a large string in Java

前端 未结 2 1104
盖世英雄少女心
盖世英雄少女心 2021-01-28 15:02

I am currently making an auction program in Java, I am trying to work out deadlines, however my date keeps coming out as (7/04/2013 11:22), is there a way to use String.format

相关标签:
2条回答
  • 2021-01-28 15:12

    @Leonard Brünings answer is the right way. And here's why your original code is the wrong way ... even if it worked.

    The javadoc for Calendar.toString() says this:

    "Return a string representation of this calendar. This method is intended to be used only for debugging purposes, and the format of the returned string may vary between implementations."

    Basically you are using toString() for a purpose that the javadoc says you shouldn't. Even if you tweaked the output from toString(), the chances are that your code would be fragile. A change in JVM could break it. A change of locale could break it.

    0 讨论(0)
  • 2021-01-28 15:28

    Simply use the SimpleDateFormat

    import java.text.SimpleDateFormat;
    
    Calendar timeOne = Server.getDateTime(itemArray.get(1).time)
    
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm")
    
    System.out.println(sdf.format(timeOne.getTime()))
    
    0 讨论(0)
提交回复
热议问题