有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从角度到Java的日期转换

您好,我正在使用当前格式为Tue Dec 07 2021 11:57:09 GMT+0530 (India Standard Time)的时刻日期 此日期作为调用api的请求参数,api如下所示:

public @ResponseBody Response updateAll( 
        @RequestParam(name = "effective-dt", required = false) Date effectiveDt) {
         /***Business Logic***/
       }

new Date()返回Tue Dec 07 11:52:52 IST 2021,angular正在尝试发送Tue Dec 07, 2021, 11:57:09 GMT+0530 (India Standard Time)。如何使angular以Java日期格式发送日期时间


共 (1) 个答案

  1. # 1 楼答案

    import {Injectable} from '@angular/core';
    
    import moment, {Moment, MomentInputObject} from 'moment/moment';
    
    @Injectable({providedIn: 'root'})
    export class ToolsDateService {
        
        convertStringToDateWithCustomFormat(date: Moment | Date | string | number | (number | string)[] | MomentInputObject, format: string): Date {
            return moment(date, format).toDate();
        }
    }
    

    如果需要更改所有日期字段,可以使用矩库处理日期 对于自定义格式,可以实现HttpInterceptor并检查 如果日期的响应数据实例将数据转换为自定义格式

    有关更多信息,请查看此链接
    Angular 5, HttpClient changes Date fields to UTC