时间类型转换的问题“2021-01-20T10:09:11Z”

本文详细指导新手如何将接收到的两种时间字符串格式('2021-01-20T10:09:11Z'和'2021-01-20T10:09:11')转换为Java Date类型,并演示了SimpleDateFormat的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于新手碰到时间类型转换的问题:
拿到的时间为字符串,格式为:“2021-01-20T10:09:11Z”。

容易理解的解决方法:
先将该字符串转换为Date类型:
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss’Z’”);
Date applyDate = sdf.parse(“2021-01-20T10:09:11Z”);

再将Date类型转换为需要的字符串类型:
SimpleDateFormat sdf1 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String stringDate= sdf1.format(applyDate);

图片为实际应用场景:
在这里插入图片描述
有的时候拿到的字符串类型为:“2021-01-20T10:09:11”,那就相应的去掉后面的’Z’就可以了。
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss”);
Date applyDate = sdf.parse(“2021-01-20T10:09:11”);

[root@master logging]# kubectl get pods -n minio -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES minio-operator-845cc4b7b6-fvsn7 1/1 Running 0 60m 10.244.3.84 node4 <none> <none> minio-operator-845cc4b7b6-lklkk 1/1 Running 0 60m 10.244.135.21 node3 <none> <none> [root@master logging]# kubectl describe tenant minio-logging -n minio Name: minio-logging Namespace: minio Labels: <none> Annotations: <none> API Version: minio.min.io/v2 Kind: Tenant Metadata: Creation Timestamp: 2025-06-13T05:32:06Z Generation: 1 Managed Fields: API Version: minio.min.io/v2 Fields Type: FieldsV1 fieldsV1: f:metadata: f:annotations: .: f:kubectl.kubernetes.io/last-applied-configuration: f:spec: .: f:configuration: f:exposeServices: .: f:console: f:minio: f:image: f:pools: .: k:{"name":"pool-0"}: .: f:name: f:resources: .: f:requests: .: f:memory: f:servers: f:volumeClaimTemplate: .: f:metadata: .: f:name: f:spec: .: f:accessModes: f:resources: .: f:requests: .: f:storage: f:storageClassName: f:volumesPerServer: Manager: kubectl-client-side-apply Operation: Update Time: 2025-06-13T05:32:06Z API Version: minio.min.io/v2 Fields Type: FieldsV1 fieldsV1: f:scheduler: .: f:name: f:status: .: f:availableReplicas: f:certificates: f:currentState: f:pools: f:revision: f:syncVersion: f:usage: Manager: minio-operator Operation: Update Subresource: status Time: 2025-06-13T05:32:11Z Resource Version: 718569 UID: 75fd24f5-1ff1-43aa-8376-1028a726fdee Spec: Configuration: Name: minio-env-config Expose Services: Console: true Minio: true Image: quay.io/minio/minio:RELEASE.2024-12-18T13-15-44Z Pools: Name: pool-0 Resources: Requests: Memory: 512Mi Servers: 1 Volume Claim Template: Metadata: Name: data Spec: Access Modes: ReadWriteOnce Resources: Requests: Storage: 5Gi Storage Class Name: openebs-hostpath Volumes Per Server: 1 Status: Available Replicas: 0 Certificates: Current State: empty tenant credentials Pools: <nil> Revision: 0 Sync Version: v6.0.0 Usage: Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning MissingCreds 51s (x2 over 61s) minio-operator Tenant is missing root credentials [root@master logging]# kubectl get pods -n minio -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES minio-operator-845cc4b7b6-fvsn7 1/1 Running 0 60m 10.244.3.84 node4 <none> <none> minio-operator-845cc4b7b6-lklkk 1/1 Running 0 60m 10.244.135.21 node3 <none> <none> [root@master logging]# kubectl exec -n minio minio-operator-845cc4b7b6-fvsn7 -- env | grep MINIO_ROOT [root@master logging]# kubectl exec -n minio minio-operator-845cc4b7b6-lklkk -- env | grep MINIO_ROOT [root@master logging]# [root@master logging]# kubectl get configmap minio-env-config -n minio -o yaml apiVersion: v1 data: MINIO_CONSOLE_ACCESS_KEY: minioadmin MINIO_CONSOLE_SECRET_KEY: minioadmin MINIO_ROOT_PASSWORD: minioadmin MINIO_ROOT_USER: minioadmin kind: ConfigMap metadata: creationTimestamp: "2025-06-13T05:12:19Z" name: minio-env-config namespace: minio resourceVersion: "715698" uid: b740d13b-f30b-4e41-8e7e-4c997e90ead5
06-14
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~~Whd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值