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

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

关于新手碰到时间类型转换的问题:
拿到的时间为字符串,格式为:“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@kubevirt1910b:~# kubectl describe vmi npu Name: npu Namespace: default Labels: kubevirt.io/domain=npu Annotations: kubevirt.io/latest-observed-api-version: v1 kubevirt.io/storage-observed-api-version: v1 kubevirt.io/vm-generation: 1 API Version: kubevirt.io/v1 Kind: VirtualMachineInstance Metadata: Creation Timestamp: 2025-11-01T10:31:11Z Finalizers: kubevirt.io/virtualMachineControllerFinalize foregroundDeleteVirtualMachine Generation: 3 Owner References: API Version: kubevirt.io/v1 Block Owner Deletion: true Controller: true Kind: VirtualMachine Name: npu UID: 9a219bef-8769-4315-9572-4efabd9ad80a Resource Version: 72186 UID: 0d67643c-9250-4f7a-9fd5-362fa209bcc8 Spec: Architecture: arm64 Domain: Cpu: Cores: 4 Model: cortex-a57 Sockets: 1 Threads: 1 Devices: Disks: Disk: Bus: virtio Name: datavolumedisk Disk: Bus: virtio Name: cloudinitdisk Host Devices: Device Name: huawei.com/Ascend910 Name: gpu0 Interfaces: Masquerade: Name: default Features: Acpi: Enabled: true Firmware: Bootloader: Efi: Secure Boot: false Uuid: 99dc9a1e-4669-5bdb-8376-0095bb6f1d64 Machine: Type: virt-rhel9.4.0 Memory: Guest: 8Gi Resources: Limits: huawei.com/Ascend910: 1 Requests: huawei.com/Ascend910: 1 Memory: 8Gi Eviction Strategy: None Networks: Name: default Pod: Node Selector: kubernetes.io/arch: arm64 node-role.kubernetes.io/npu: true node.kubernetes.io/cpu-model: cortex-a57 Volumes: Data Volume: Name: arm64-ubuntu-test Name: datavolumedisk Cloud Init No Cloud: User Data: #cloud-config manage_etc_hosts: true hostname: npu ssh_pwauth: true chpasswd: expire: false list: | root:ubuntu users: - name: root lock_passwd: false sudo: ALL=(ALL) NOPASSWD:ALL shell: /bin/bash Name: cloudinitdisk Status: Active Pods: 23e296b7-aaa7-4b39-aa51-36240b3806a6: Conditions: Last Probe Time: 2025-11-01T10:31:11Z Last Transition Time: 2025-11-01T10:31:11Z Message: Guest VM is not reported as running Reason: GuestNotRunning Status: False Type: Ready Last Probe Time: <nil> Last Transition Time: <nil> Message: All of the VMI's DVs are bound and not running Reason: AllDVsReady Status: True Type: DataVolumesReady Last Probe Time: <nil> Last Transition Time: 2025-11-01T10:31:11Z Message: 0/1 nodes are available: 1 node(s) didn't match Pod's node affinity/selector. preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling.. Reason: Unschedulable Status: False Type: PodScheduled Current CPU Topology: Cores: 4 Sockets: 1 Threads: 1 Guest OS Info: Launcher Container Image Version: quay.io/kubevirt/virt-launcher:v1.4.0 Memory: Guest At Boot: 8Gi Guest Current: 8Gi Guest Requested: 8Gi Phase: Scheduling Phase Transition Timestamps: Phase: Pending Phase Transition Timestamp: 2025-11-01T10:31:11Z Phase: Scheduling Phase Transition Timestamp: 2025-11-01T10:31:11Z Qos Class: Burstable Runtime User: 107 Virtual Machine Revision Name: revision-start-vm-9a219bef-8769-4315-9572-4efabd9ad80a-1 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 7m18s virtualmachine-controller Created virtual machine pod virt-launcher-npu-2n86f
最新发布
11-02
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

~~Whd

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

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

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

打赏作者

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

抵扣说明:

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

余额充值