- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to parse date and time
Create a SimpleDateFormat object −
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
Do not forget to import the following package for SimpleDateFormat class −
import java.text.SimpleDateFormat;
Now, since we have set the format for date above, let us parse the date using parseObject() method −
Date dt = (Date) dateFormat.parseObject("2018.11.22.11.50.15");
The following is an example −
Example
import java.util.Date; import java.text.SimpleDateFormat; public class Demo { public static void main(String[] argv) throws Exception { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); // parse System.out.println("Parse Date and Time..."); Date dt = (Date) dateFormat.parseObject("2018.11.22.11.50.15"); System.out.println(dt); } }
Output
Parse Date and Time... Thu Nov 22 11:50:15 UTC 2018
Advertisements