Java – How to format string using SampleDateFormat ?


One of the blog readers asked me this question, how to format string using SampleDateFormat to mm/dd/yyy.

Here is a small piece of code, I hope some of you will find it useful.

a. Frist – import 2 libraries:


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

b. Here is code:


Date date = new Date(); // creating date object
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy hh:mm a");
// creating SampleDate object and passing the pattern to constructor ("a" means show AM or PM)
System.out.println( "Here is today's date: " +  formatter.format(date) + ");

//The output: Here is today's date: 04/06/12 10:52 AM

Regards,

Anatoly Spektor

Advertisements

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module, Red Hat and some other places :) I am an author of the book 'Eclipse Debugging How To', Muay Thai fighter and amateur photographer ;)
This entry was posted in Java, open-source, Useful Functions and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Java – How to format string using SampleDateFormat ?

  1. Here we can elaborate the J2ME implementation of popular protocols adapted for TC65i and other Protocols like POP3, FTP, and SMTP etc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s