当前位置:网站首页>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
边栏推荐
- Mysql - CRUD
- 18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
- [designmode] Decorator Pattern
- Solve the problem of reading Chinese garbled code in sqlserver connection database
- MySQL global lock and table lock
- [binary search tree] add, delete, modify and query function code implementation
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
- Classical concurrency problem: the dining problem of philosophers
- Extracting profile data from profile measurement
- Qt QPushButton详解
猜你喜欢
Go learning - dependency injection
[online chat] the original wechat applet can also reply to Facebook homepage messages!
LeetCode 1598. Folder operation log collector
Wechat applet -- wxml template syntax (with notes)
MySql——CRUD
OS i/o devices and device controllers
The difference of time zone and the time library of go language
GD32F4xx uIP协议栈移植记录
Huawei equipment configuration ospf-bgp linkage
About the slmgr command
随机推荐
MySQL functions
硬件及接口学习总结
【QT】Qt使用QJson生成json文件并保存
Permission problem: source bash_ profile permission denied
Knowledge about the memory size occupied by the structure
How much do you know about the bank deposit business that software test engineers must know?
What are the functions of Yunna fixed assets management system?
Hudi of data Lake (1): introduction to Hudi
【GYM 102832H】【模板】Combination Lock(二分图博弈)
Effet Doppler (déplacement de fréquence Doppler)
7.5 simulation summary
MySQL之函数
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
The difference of time zone and the time library of go language
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
【DesignMode】装饰者模式(Decorator pattern)
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Codeforces round 804 (Div. 2) [competition record]
数据库遇到的问题
Go learning --- read INI file