日期格式[2020,9,15]:这是什么类型的格式?

问题描述

我正在尝试使用具有一些日期的外部json。日期的格式为:[ 2020,9,15 ]我尝试将它用作字符串,但是没有用。

能告诉我这是哪种格式

解决方法

java.time

将JSON数字数组读入Java <mat-form-field appearance="fill"> <mat-label>Umkreis</mat-label> <mat-select panelClass="test" [(ngModel)]="selectedRadius" name="radius"> <mat-option *ngFor="let radius of radiusList" [value]="radius.value"> {{radius.viewValue}} </mat-option> </mat-select> </mat-form-field>int[]数组)中,并从中构造一个int

LocalDate

输出为:

    int[] arrayFromJson = { 2020,9,15 };
    System.out.println("Array from JSON: " + Arrays.toString(arrayFromJson));
    
    LocalDate date = LocalDate.of(arrayFromJson[0],arrayFromJson[1],arrayFromJson[2]);
    System.out.println("Date as LocalDate: " + date);

Array from JSON: [2020,15] Date as LocalDate: 2020-09-15 是java.time(现代Java日期和时间API)中的类,用于表示没有时间的日期,因此在这里可以使用正确的类。

读取和解析JSON

如何将JSON读入Java?这取决于您要使用哪个库。这是使用杰克逊的示例:

LocalDate
    ObjectMapper mapper = new ObjectMapper();
    String json = "[ 2020,15]";

    int[] arrayFromJson = mapper.readValue(json,int[].class);
    System.out.println(Arrays.toString(arrayFromJson));