日付と時刻の書式設定

DataWeave を使用して、入力した日付と時刻の形式を変更できます。 開始する前に、DataWeave バージョン 2 (​%dw 2.0​) は Mule 4 アプリケーションを対象とすることに注意してください。Mule 3 アプリケーションの場合、Mule 3.9 ドキュメントセットの DataWeave バージョン 1 (​%dw 1.0​) の例を参照してください。他の Mule バージョンの場合は、目次の Mule Runtime バージョンセレクターを使用できます。

日付と時刻の形式の変更

MM​ や ​dd​ などの書式設定文字を組み合わせて、サポートされる日付と時刻の形式を記述できます。この例では、​as​ 演算子を使用して日付と時刻を文字列として記述します。

変換
%dw 2.0
output application/json
---
{
  formattedDate: |2020-10-01T23:57:59| as String {format: "uuuu-MM-dd"},
  formattedTime: |2020-10-01T23:57:59| as String {format: "KK:mm:ss a"},
  formattedDateTime: |2020-10-01T23:57:59| as String {format: "KK:mm:ss a, MMMM dd, uuuu"}
}
出力
{
  "formattedDate": "2020-10-01",
  "formattedTime": "11:57:59 PM",
  "formattedDateTime": "11:57:59 PM, October 01, 2020"
}

DataWeave 型としてのカスタム日付形式の作成

スクリプトで複数の似た変換を実行する場合、ヘッダーでカスタム型をディレクティブとして定義し、各日付をその型に設定できます。DataWeave 型の名前では、大文字と小文字が区別されます。

DataWeave スクリプト
%dw 2.0
output application/json
type Mydate = String { format: "uuuu/MM/dd" }
---
{
  formattedDate1: |2019-10-01T23:57:59| as Mydate,
  formattedDate2: |2020-07-06T08:53:15| as Mydate
}
出力
{
  "formattedDate1": "2019/10/01",
  "formattedDate2": "2020/07/06"
}

日付と時刻の書式設定文字の使用

DataWeave では、日付形式 ​uuuu-MM-dd​ での ​u​ (年)、​M​、​d​ など、書式設定文字の使用がサポートされています。これらの文字は、Java 8 の ​java.time.format​ パッケージに基づいています。

次の例は、サポートされる文字を表示するように ​now​ DataWeave 関数の出力を書式設定しています。

DataWeave スクリプト
%dw 2.0
var myDateTime = ("2020-11-10T13:44:12.283-08:00" as DateTime)
output application/json
---
{
    "dateTime" : myDateTime,
    "era-G" : myDateTime as String { format: "G"},
    "year-u" : myDateTime as String {format: "u"},
    "year-uu" : myDateTime as String {format: "uu"},
  //y is for use with the era (BCE or CE). Generally, use u, instead.
    "year-y" : myDateTime as String { format: "y"},
    "year-yy" : myDateTime as String { format: "yy"},
    "dayOfYear-D" : myDateTime as String { format: "D"},
    "monthOfYear-MMMM": myDateTime as String { format: "MMMM"},
    "monthOfYear-MMM": myDateTime as String { format: "MMM"},
    "monthOfYear-MM": myDateTime as String { format: "MM"},
    "monthOfYear-M": myDateTime as String { format: "M"},
    "monthOfYear-LL": myDateTime as String { format: "LL"},
    "monthOfYear-L": myDateTime as String { format: "L"},
    "dayOfMonth-d" : myDateTime as String {format: "d"},
    "quarterOfYear-qqq" : myDateTime as String {format: "qqq"},
    "quarterOfYear-qq" : myDateTime as String {format: "qq"},
    "quarterOfYear-q" : myDateTime as String {format: "q"},
    "quarterOfYear-QQQQ" : myDateTime as String {format: "QQQQ"},
    "quarterOfYear-QQQ" : myDateTime as String {format: "QQQ"},
    "quarterOfYear-QQ" : myDateTime as String {format: "QQ"},
    "quarterOfYear-Q" : myDateTime as String {format: "Q"},
  // Understand "Y" and "YY" thoroughly before using it.
    "weekBasedYear-YY" : myDateTime as String { format: "YY"},
    "weekBasedYear-Y" : myDateTime as String { format: "Y"},
    "weekInYear-w" : myDateTime as String {format: "w"},
    "weekInMonth-W" : myDateTime as String {format: "W"},
    "dayOfWeekAbbreviatedName-E" : myDateTime as String {format: "E"},
    "dayOfWeekFullName-EEEE" : myDateTime as String {format: "EEEE"},
    "localizedDayOfWeek-eeee" : myDateTime as String {format: "eeee"},
    "localizedDayOfWeek-eee" : myDateTime as String {format: "eee"},
    "localizedDayOfWeek-ee" : myDateTime as String {format: "ee"},
    "localizedDayOfWeek-e" : myDateTime as String {format: "e"},
    "localizedDayOfWeek-cccc" : myDateTime as String {format: "cccc"},
    "localizedDayOfWeek-ccc" : myDateTime as String {format: "ccc"},
    "localizedDayOfWeek-c" : myDateTime as String {format: "c"},
    "weekOfMonth-F" : myDateTime as String {format: "F"},
    "amORpm-a" : myDateTime as String {format: "a"},
  // "h" outputs 12 o'clock as 12. Other hours match "K" output.
    "hourOfDay1to12-h" : myDateTime as String {format: "h"},
  // "K" outputs 12 o'clock as 0. Other hours match "h" output.
    "hourOfDay0to11-K" : myDateTime as String {format: "K"},
    "clockHourOfAmPm-k" : myDateTime as String {format: "k"},
    "hourOfDay0to23-H" : myDateTime as String {format: "H"},
    "minuteOfHour-m" : myDateTime as String {format: "m"},
    "secondOfMinute-s" : myDateTime as String {format: "s"},
    "fractionOfSecond-S" : myDateTime as String {format: "S"},
    "millisecondOfDay-A" : myDateTime as String {format: "A"},
    "nanosecondCountOfSecond-n" : myDateTime as String {format: "n"},
    "nanosecondCountOfDay-N" : myDateTime as String {format: "N"},
    "timeZoneID-VV" : myDateTime as String {format: "VV"},
    "timeZoneName-zz" : myDateTime as String {format: "zz"},
    "localizedZoneOffset-zzz" : myDateTime as String {format: "zzz"},
    "localizedZoneOffset-O" : myDateTime as String {format: "O"},
    "timeZoneOffsetZforZero-XXX" : myDateTime as String {format: "XXX"},
    "timeZoneOffsetZforZero-XX" : myDateTime as String {format: "XX"},
    "timeZoneOffsetZforZero-X" : myDateTime as String {format: "X"},
    "timeZoneOffset-xxx" : myDateTime as String {format: "xxx"},
    "timeZoneOffset-xx" : myDateTime as String {format: "xx"},
    "timeZoneOffset-x" : myDateTime as String {format: "x"},
    "timeZoneOffset-Z" : myDateTime as String {format: "Z"}
 }

日付または時刻を書式設定するための構文の使用方法を参照してください。

出力
{
  "dateTime": "2020-11-10T13:44:12.283-08:00",
  "era-G": "AD",
  "year-u": "2020",
  "year-uu": "20",
  "year-y": "2020",
  "year-yy": "20",
  "dayOfYear-D": "315",
  "monthOfYear-MMMM": "November",
  "monthOfYear-MMM": "Nov",
  "monthOfYear-MM": "11",
  "monthOfYear-M": "11",
  "monthOfYear-LL": "11",
  "monthOfYear-L": "11",
  "dayOfMonth-d": "10",
  "quarterOfYear-qqq": "4",
  "quarterOfYear-qq": "04",
  "quarterOfYear-q": "4",
  "quarterOfYear-QQQQ": "4th quarter",
  "quarterOfYear-QQQ": "Q4",
  "quarterOfYear-QQ": "04",
  "quarterOfYear-Q": "4",
  "weekBasedYear-YY": "20",
  "weekBasedYear-Y": "2020",
  "weekInYear-w": "46",
  "weekInMonth-W": "2",
  "dayOfWeekAbbreviatedName-E": "Tue",
  "dayOfWeekFullName-EEEE": "Tuesday",
  "localizedDayOfWeek-eeee": "Tuesday",
  "localizedDayOfWeek-eee": "Tue",
  "localizedDayOfWeek-ee": "03",
  "localizedDayOfWeek-e": "3",
  "localizedDayOfWeek-cccc": "Tuesday",
  "localizedDayOfWeek-ccc": "Tue",
  "localizedDayOfWeek-c": "3",
  "weekOfMonth-F": "3",
  "amORpm-a": "PM",
  "hourOfDay1to12-h": "1",
  "hourOfDay0to11-K": "1",
  "clockHourOfAmPm-k": "13",
  "hourOfDay0to23-H": "13",
  "minuteOfHour-m": "44",
  "secondOfMinute-s": "12",
  "fractionOfSecond-S": "2",
  "millisecondOfDay-A": "49452283",
  "nanosecondCountOfSecond-n": "283000000",
  "nanosecondCountOfDay-N": "49452283000000",
  "timeZoneID-VV": "-08:00",
  "timeZoneName-zz": "-08:00",
  "localizedZoneOffset-zzz": "-08:00",
  "localizedZoneOffset-O": "GMT-8",
  "timeZoneOffsetZforZero-XXX": "-08:00",
  "timeZoneOffsetZforZero-XX": "-0800",
  "timeZoneOffsetZforZero-X": "-08",
  "timeZoneOffset-xxx": "-08:00",
  "timeZoneOffset-xx": "-0800",
  "timeZoneOffset-x": "-08",
  "timeZoneOffset-Z": "-0800"
}