HarmonyOS鸿蒙Next中X509证书有效期起始时间、截止时间怎么转换为年月日格式

HarmonyOS鸿蒙Next中X509证书有效期起始时间、截止时间怎么转换为年月日格式

// 证书有效期起始日期 let notBeforeTime = x509Cert.getNotBeforeTime(); // 证书有效期截止日期 let notAfterTime = x509Cert.getNotAfterTime(); Logger.info(‘notBeforeTime=:’, notBeforeTime); Logger.info(‘notAfterTime=:’, notAfterTime);

怎么把notBeforeTime和notAfterTime转成年月日格式呢?


更多关于HarmonyOS鸿蒙Next中X509证书有效期起始时间、截止时间怎么转换为年月日格式的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

X509证书有效期起始时间(格式:YYMMDDHHMMSSZ 或 YYYYMMDDHHMMSSZ,时间以Z结尾:表示标准时间) 参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-cert-V5#getnotbeforetime

文档中获取的时间格式和证书中的时间格式有关,文档两种时间格式是代表证书里面的时间格式是两种形式; 当前数字证书中的有效期(生效时间、失效时间)等日期的定义,可以使用两种类型:UTCTime和GeneralizedTime 其中UTCTime类型由于设计的问题(仅使用了两位数字表示年份),只能代表 1950-01-01~2049-12-31内的日期,且精度仅到秒; 有效期是一个时间序列,UTCTime的格式是YYMMDDHHMMSSZ ,GeneralizedTime的格式是YYYYMMDDHHMMSSZ 在2050年以后的证书时间必须要使用GeneralizedTime, 接口返回的是按照证书内时间格式返回。

可使用这种方式转换为YEAR-MONTH-DAY字符串

function getFormatTime(str: string): string {
  if (str?.length === 13) {
    let dateStr = ''
    for (let index = 0; index < 3; index++) {
      if (index === 0) {
        if (parseInt(str?.slice(0, 2)) > 49) {
          dateStr = '19' + str?.slice(index, index + 2)
        } else {
          dateStr = '20' + str?.slice(index, index + 2)
        }
      } else {
        dateStr = dateStr + '-' + str?.slice(index * 2, index * 2 + 2)
      }
    }
    return dateStr;
  } else {
    return str;
  }
}

更多关于HarmonyOS鸿蒙Next中X509证书有效期起始时间、截止时间怎么转换为年月日格式的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,X509证书的有效期起始时间和截止时间可以通过X509Certificate类中的getNotBefore()getNotAfter()方法获取。这两个方法返回的是Date对象,可以通过Date类的方法转换为年月日格式。

例如,以下代码展示了如何将证书的有效期起始时间和截止时间转换为yyyy-MM-dd格式:

import securityCert from '@ohos.security.cert';

let cert: securityCert.X509Certificate = ...; // 获取X509证书对象
let notBefore: Date = cert.getNotBefore(); // 获取有效期起始时间
let notAfter: Date = cert.getNotAfter(); // 获取有效期截止时间

let formattedNotBefore: string = notBefore.toISOString().split('T')[0]; // 转换为yyyy-MM-dd格式
let formattedNotAfter: string = notAfter.toISOString().split('T')[0]; // 转换为yyyy-MM-dd格式

toISOString()方法将Date对象转换为ISO 8601格式的字符串,split('T')[0]则提取出日期部分。

在HarmonyOS鸿蒙Next中,可以使用X509Certificate类的getNotBefore()getNotAfter()方法获取证书的有效期起始时间和截止时间。这些方法返回Date对象,可以通过SimpleDateFormat类将其转换为年月日格式。示例代码如下:

import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CertificateDateConverter {
    public static void main(String[] args) throws Exception {
        // 假设你已经获取了X509Certificate对象
        X509Certificate cert = ...;

        Date notBefore = cert.getNotBefore();
        Date notAfter = cert.getNotAfter();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = sdf.format(notBefore);
        String endDate = sdf.format(notAfter);

        System.out.println("有效期起始时间: " + startDate);
        System.out.println("有效期截止时间: " + endDate);
    }
}

这段代码将证书的有效期起始时间和截止时间转换为yyyy-MM-dd格式的字符串。

回到顶部