当前位置:网站首页>Date类中日期转成指定字符串出现的问题及解决方法
Date类中日期转成指定字符串出现的问题及解决方法
2022-07-06 00:17:00 【王小小鸭】
在学习Date内容时跟着做练习,结果老是报错,我寻思着代码也没错鸭,难道是我导的包不正确,
反反复复查错找问题都确认代码没问题,那问题出在哪里?我陷入了沉默……

好在在我火眼金睛的加持下终于找到,上面的11:20:00用的英文输入法的冒号,下面时分秒用的是中文输入法的冒号,自然匹配不了,日期和模式必须一一对照

改正成英文输入法后成功编译运行

sdf2.parse(ss); 快速补全 Ctrl+lt+v
//ParseException 报错,解析异常 解决的快捷键 Alt+Enter,默认第一个附上完整代码:
package com.com.object_11.APITest_01.Date;
//有毒毒
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*SimpleDateFormat类构造方法
public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式
public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat使用给定的模式和默认的日期格式
- SimpleDateFormat类的常用方法
- 格式化(从Date到String)
- public final String format(Date date):将日期格式化成日期/时间字符串
- 解析(从String到Date)
- public Date parse(String source):从给定字符串的开始解析文本以生成日期*/
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
// 格式化(从Date到String)
// 无参构造创建对象
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(); //无参构造
//创造方法 sdf.format(d);然后Ctrl+Alt+v自动生成
String s = sdf.format(d);
// 输出这个字符串
System.out.println(s);
//2022/6/29 上午11:03 (默认的模式)
System.out.println("-----------");
//格式化(从Date到String)
// 无参构造创建对象
Date d1 = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat();
// 去掉无参构造,自己写
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //年月日时分秒
//创造方法 sdf.format(d1);然后Ctrl+Alt+v自动生成
String s1 = sdf.format(d1);
// 输出这个字符串
System.out.println(s1);
// (默认的模式)
System.out.println("-----------");
// (从String到Date)
String ss = "2025-06-29 11:20:00";
//ParseException 报错,解析异常
// SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}
//2022/6/30 下午5:00
//-----------
//2022/6/30 下午5:00
//-----------
//Sun Jun 29 11:20:00 CST 2025
边栏推荐
- China Jinmao online electronic signature, accelerating the digitization of real estate business
- 【在线聊天】原来微信小程序也能回复Facebook主页消息!
- 认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
- 行列式学习笔记(一)
- 云呐|公司固定资产管理系统有哪些?
- 关于slmgr命令的那些事
- How much do you know about the bank deposit business that software test engineers must know?
- Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- [gym 102832h] [template] combination lock (bipartite game)
- Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
猜你喜欢

Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)

Yunna | what are the main operating processes of the fixed assets management system

How much do you know about the bank deposit business that software test engineers must know?

openssl-1.0.2k版本升级openssl-1.1.1p

LeetCode 1598. Folder operation log collector

教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!

MySql——CRUD
![[designmode] Decorator Pattern](/img/65/457e0287383d0ca9a28703a63b4e1a.png)
[designmode] Decorator Pattern

Miaochai Weekly - 8

Gd32f4xx UIP protocol stack migration record
随机推荐
OS i/o devices and device controllers
软件测试工程师必会的银行存款业务,你了解多少?
notepad++正则表达式替换字符串
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
Qt QPushButton详解
微信小程序---WXML 模板语法(附带笔记文档)
Key structure of ffmpeg - avformatcontext
FFMPEG关键结构体——AVFormatContext
【DesignMode】组合模式(composite mode)
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
AtCoder Beginner Contest 258【比赛记录】
7.5 simulation summary
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
电机的简介
FFMPEG关键结构体——AVCodecContext
转:未来,这样的组织才能扛住风险
The difference of time zone and the time library of go language
Hudi of data Lake (1): introduction to Hudi
LeetCode 6005. The minimum operand to make an array an alternating array