Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ How to turn Java String into Date     - Upgrade to Linux Mint 16 petra Mint 17 Qiana (Linux)

- Getting Started with Linux: Learn how to upgrade Docker in Ubuntu (Server)

- React Native (Programming)

- [SHELL] MySQL primary recovery solution from + Keepalived online (Server)

- Java Foundation - Variables and data types (Programming)

- Oracle Execute to Parse perform analytical Ratio Analysis (Database)

- Use top to monitor the remote server (Server)

- Linux system monitoring, top command of the diagnostic tool Detailed (Linux)

- Redis is installed and set up Ubuntu 14.04 from the environment under the main ssdb (Server)

- Unix / Linux commonly used to clean up disk space command (Linux)

- MySQL server after an unexpected power outage can not start (Database)

- MySQL database under Linux to achieve automatic backup scheduled backup day (Database)

- Lambda expressions of evolution (Programming)

- grep command usage (Linux)

- iscsiadm command usage (Linux)

- MongoDB slice simple example (Database)

- To install PostgreSQL 9.4 (Database)

- To configure and use multiple Tomcat under Linux environment (Server)

- To install and use the Doxygen under Linux (Linux)

- Linux productivity tools and tips (Linux)

 
         
  How to turn Java String into Date
     
  Add Date : 2017-01-08      
         
       
         
  Recently discovered spring mvc and Jackson on Date type of support is not particularly good, although the Java object serialization Json data into the process provides a convenient annotation, but deserialization but in developing Json data deserialized into Java objects when no, so he begs the following question, I need to manually convert the string to Date data type, java.text.SimpleDateFormat provides us with the infrastructure of such conversion. Simply defined as a list of formatted mode, if the 'M' not less than three, month is displayed as the letter, otherwise it is digital. Please refer to a more detailed definition java.text.SimpleDateFormat JavaDoc.

Letter Description Examples
y Year 2015
M Month in year Nov, 11, 7
d Day in month 1-31
E Day name in week Friday, Sunday
a Am / pm marker AM, PM
H Hour in day 0-23
h Hour in am / pm 1-12
m Minute in hour 0-60
s Second in minute 0-60

 

1. Date = 7-Nov-2015

    SimpleDateFormat formatter = new SimpleDateFormat ( "dd-MMM-yyyy");
    String dateInString = "7-Nov-2015";

    try {

        Date date = formatter.parse (dateInString);
        System.out.println (date);
        System.out.println (formatter.format (date));

    } Catch (ParseException e) {
        e.printStackTrace ();
    }
    
Output

Mon Nov 07 00:00:00 MYT 2015
07-Nov-2015


2. Date = 07/06/2015

    SimpleDateFormat formatter = new SimpleDateFormat ( "dd / MM / yyyy");
    String dateInString = "07/06/2015";

    try {

        Date date = formatter.parse (dateInString);
        System.out.println (date);
        System.out.println (formatter.format (date));

    } Catch (ParseException e) {
        e.printStackTrace ();
    }
    
Output
Mon Nov 07 00:00:00 MYT 2015
07/06/2015

3. Date = Nov 7, 2015

    SimpleDateFormat formatter = new SimpleDateFormat ( "MMM dd, yyyy");
    String dateInString = "Nov 7, 2015";

    try {

        Date date = formatter.parse (dateInString);
        System.out.println (date);
        System.out.println (formatter.format (date));

    } Catch (ParseException e) {
        e.printStackTrace ();
    }
Output

Mon Nov 07 00:00:00 MYT 2015
Nov 07, 2015

4. Date = Fri, Nov 7 2015

    SimpleDateFormat formatter = new SimpleDateFormat ( "E, MMM dd yyyy");
    String dateInString = "Fri, Nov 7 2015";
        
    try {

        Date date = formatter.parse (dateInString);
        System.out.println (date);
        System.out.println (formatter.format (date));

    } Catch (ParseException e) {
        e.printStackTrace ();
    }
    
Output
Mon Nov 07 00:00:00 MYT 2015
Mon, Nov 07 2015

 
2. Date and Time Example
1. Date and Time = Friday, Nov 7, 2015 12:10:56 PM

    SimpleDateFormat formatter = new SimpleDateFormat ( "EEEE, MMM dd, yyyy HH: mm: ss a");
    String dateInString = "Friday, Nov 7, 2015 12:10:56 PM";
        
    try {

        Date date = formatter.parse (dateInString);
        System.out.println (date);
        System.out.println (formatter.format (date));

    } Catch (ParseException e) {
        e.printStackTrace ();
    }
    
Output
Mon Nov 07 12:10:56 MYT 2015
Monday, Nov 07, 2015 12:10:56 PM

to sum up
This paper briefly summarizes the string into Java objects mainstream method, we want to help.
     
         
       
         
  More:      
 
- Ubuntu batch scp to copy files without password (Linux)
- Use SecureCRT to transfer files between local and remote hosts (Linux)
- Shell Script: Bulk add users, and set the random character password (Programming)
- Use smartmontools view the health status of hard disk (Linux)
- Android gets the global process information and the memory used by the process (Programming)
- Use XtraBackup to MySQL database online incremental backup and recovery (Database)
- To protect the temporary files for Linux security (Linux)
- installation and configuration of the PHP environment (Apache2) under Linux (Server)
- Linux system boot process detail (Linux)
- Linux, modify the fstab file system can not start causing solve one case (Linux)
- Linux System Getting Started Tutorial: How to Force Change your password at next logon Linux (Linux)
- Effect MongoDB plan cache (Database)
- Create Your Own Docker base image in two ways (Linux)
- Tor Browser: under Linux for anonymous Web browsing ultimate browser (Linux)
- MySQL flip-flop (Database)
- HTTP and FTP TCP-FLOOD CC Attacks Analysis and Prevention solutions under CentOS 6.5 (Linux)
- Oracle database import and export (Database)
- OpenJDK 7 compiled under Ubuntu 14.04.3 64-bit (Linux)
- C # assembly calls across constants, variables and functions (Programming)
- Create a custom pixel format based on an existing image data BufferedImage (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.