如何在数字前打印全年和零的本地化日期?

时间:2015-08-11 15:20:36

标签: java jodatime

对于我的Java应用程序,我使用Joda Time库来管理日期。现在我用我的代码获得了以下结果:

<div class="list card">
    <div class="item stable-bg">
        <h2 align="center">Initial Strike Water Calculator</h2>
        <p></p>
    </div>
    <div class="item item-body" align="left" ng-controller="CalcCtrl">
        <label class="item item-input">
            <span class="input-label"><b>Initial Gallons of Water</b></span>
            <input type="tel" placeholder="5 gal" ng-model="startingWater">
        </label>
        <label class="item item-input">
            <span class="input-label"><b>Pounds of Grain Required</b></span>
            <input type="tel" placeholder="10 lbs" ng-model="lbsOfGrain">
        </label>
        <label class="item item-input">
            <span class="input-label"><b>Initial Temperature of Dry Grain</b></span>
            <input type="tel" placeholder="70 &deg; F" ng-model="grainTemp">
        </label>
        <label class="item item-input">
            <span class="input-label"><b>Target Temperature of Mash</b></span>
            <input type="tel" placeholder="155 &deg; F" ng-model="targetTemp">
        </label>
        <label class="item item-output">
            <span class="text"><b>Required Infusion Water Temperature</b></span>
            <span class="result" placeholder="Ex. 170 &deg; F"><b>&nbsp &nbsp &nbsp &nbsp{{iStrikeResult}} &deg; F</b></span>
        </label>
        <p></p>

        <button class="button icon ion-beer" ng-click="iniStrikeCalc()">&nbsp Calculate</button>  
    </div>
</div>

其中11是日,8是月,15是年;我在西班牙。我希望我的代码打印的是:

11/8/15

即,打印数字&lt; 10,零和数字,并显示年份的四位数。

我的代码如下:

11/08/2015

我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

只需使用DateTimeFormat.forPattern("dd/MM/yyyy");

即可

请参阅:DateTimeFormatter forPattern(String pattern)

示例代码:

public class DateExample {
  public static void main(String[] args) {
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy");
    String date = fmt.print(new DateTime(2015, 8, 11, 0, 0));
    System.out.print(date);
  }
}

输出:

11/08/2015

答案 1 :(得分:0)

Joda Time库允许您提供custom formatter

comparison_time=$(date -j 0120.50 +"%s")