Tuesday, March 4, 2014

Convert Date format

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test2 {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        //set date to 2014, January 12 16:25:20
        calendar.set(2014, 0, 12, 16, 25, 20);
        Date date = calendar.getTime();

        //format to MM/dd/YYY HH:mm.ss (24 hour format)
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/YYY HH:mm.ss");
        String dateString = sdf1.format(date);
        System.out.println(dateString);

        //format to MM/dd/YYY hh:mm.ss aa (12 hour format)
        sdf1 = new SimpleDateFormat("MM/dd/YYY hh:mm.ss aa");
        dateString = sdf1.format(date);
        System.out.println(dateString);
    }

}

Reference:

1. SimpleDateFormat
2. Creating and converting Date and Time in Java 8

----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment