当前位置:网站首页>Problems and solutions of converting date into specified string in date class
Problems and solutions of converting date into specified string in date class
2022-07-06 00:22:00 【Wang Xiaoya】
I'm learning Date Follow the exercise as you read the content , The result is always wrong , I think the code is right, duck , Is it because my bag is incorrect ,
Check the code again and again to make sure the code is ok , What's the problem ? I fell into silence ……
Fortunately, with the blessing of my eyes, I finally found , above 11:20:00 Colon of English input method used , The following hours, minutes and seconds use the colon of the Chinese input method , Nature cannot match , Dates and patterns must be compared one by one
After correcting it into English input method, it is compiled and run successfully
sdf2.parse(ss); Quick completion Ctrl+lt+v
//ParseException Report errors , Parsing exceptions Shortcut keys for solving Alt+Enter, Default first
Attach complete code :
package com.com.object_11.APITest_01.Date;
// Toxic poison
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*SimpleDateFormat Class constructor
public SimpleDateFormat() Construct a SimpleDateFormat, Use default mode and date format
public SimpleDateFormat(String pattern) Construct a SimpleDateFormat Use the given pattern and default date format
- SimpleDateFormat Common methods of class
- format ( from Date To String)
- public final String format(Date date): Format the date as a date / Time string
- analysis ( from String To Date)
- public Date parse(String source): Parse the text from the beginning of the given string to generate the date */
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
// format ( from Date To String)
// Parameterless construction creates objects
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(); // No arguments structure
// How to create sdf.format(d); then Ctrl+Alt+v Automatic generation
String s = sdf.format(d);
// Output this string
System.out.println(s);
//2022/6/29 In the morning 11:03 ( Default mode )
System.out.println("-----------");
// format ( from Date To String)
// Parameterless construction creates objects
Date d1 = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat();
// Remove the nonparametric structure , Write it yourself
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy year MM month dd Japan HH:mm:ss"); // Mm / DD / yyyy HHM / S
// How to create sdf.format(d1); then Ctrl+Alt+v Automatic generation
String s1 = sdf.format(d1);
// Output this string
System.out.println(s1);
// ( Default mode )
System.out.println("-----------");
// ( from String To Date)
String ss = "2025-06-29 11:20:00";
//ParseException Report errors , Parsing exceptions
// SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy year MM month dd Japan 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 Afternoon 5:00
//-----------
//2022/6/30 Afternoon 5:00
//-----------
//Sun Jun 29 11:20:00 CST 2025
边栏推荐
- [designmode] Decorator Pattern
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- Upgrade openssl-1.1.1p for openssl-1.0.2k
- Priority queue (heap)
- AtCoder Beginner Contest 258【比赛记录】
- What is information security? What is included? What is the difference with network security?
- LeetCode 8. String conversion integer (ATOI)
- 【DesignMode】装饰者模式(Decorator pattern)
- Detailed explanation of APP functions of door-to-door appointment service
猜你喜欢
NSSA area where OSPF is configured for Huawei equipment
OpenCV经典100题
STM32 configuration after chip replacement and possible errors
时区的区别及go语言的time库
MDK debug时设置数据实时更新
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
Classic CTF topic about FTP protocol
Yunna | what are the main operating processes of the fixed assets management system
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Knowledge about the memory size occupied by the structure
随机推荐
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
notepad++正則錶達式替換字符串
LeetCode 6006. Take out the least number of magic beans
An understanding of & array names
Notepad + + regular expression replace String
Wechat applet -- wxml template syntax (with notes)
Atcoder beginer contest 258 [competition record]
QT -- thread
7.5模拟赛总结
云呐|公司固定资产管理系统有哪些?
Gd32f4xx UIP protocol stack migration record
wx. Getlocation (object object) application method, latest version
NSSA area where OSPF is configured for Huawei equipment
7.5 simulation summary
How much do you know about the bank deposit business that software test engineers must know?
OpenCV经典100题
Room cannot create an SQLite connection to verify the queries
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
如何利用Flutter框架开发运行小程序
LeetCode 斐波那契序列