Techie Hints

A place where to find tech solution

How to parse a date in different format ?

Here is very good example for Java developers to parse date as per requirement.

// This is how to get today’s date in Java
Date today = new Date();

//If you print Date, you will get un formatted output
System.out.println(“Today is : ” + today);

// Prints: Today is : Tue Dec 29 18:39:06 IST 2015

//formatting date in Java using SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(“dd-MM-yyyy”);
String date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yyyy format : ” + date);

// Prints: Today in dd-MM-yyyy format : 29-12-2015
//Another Example of formatting Date in Java using SimpleDateFormat
DATE_FORMAT = new SimpleDateFormat(“dd/MM/yy”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd/MM/yy pattern : ” + date);

// Prints: Today in dd/MM/yy pattern : 29/12/15

//formatting Date with time information
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SS”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yy:HH:mm:SS : ” + date);

// Prints: Today in dd-MM-yy:hh:mm:SS : 29-12-15:18:39:40

// Date with timezone information
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SSZ”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yy:HH:mm:SSZ : ” + date);

// Prints: Today in dd-MM-yy:HH:mm:SSZ : 29-12-15:18:39:40+0530

 

 

2 thoughts on “How to parse a date in different format ?

    1. @Praneeth

      Let me give you quick solution.
      Use java.util.Calendar,

      Just copy following method to your code:

      public Date getNextDates(int requiredNumberOfNextDate) {
      Calendar calendar = Calendar.getInstance(); // get a calendar instance, which defaults to “now”
      calendar.add(Calendar.DAY_OF_YEAR, requiredNumberOfNextDate); // add one day to the date/calendar
      Date nextDate = calendar.getTime(); // Gives you the java.util.date

      return nextDate
      }

      Following code explains in detail

      int amountOfDateToAdd = 2; // Just keep changing the number to get next number of days date

      // get a calendar instance, which defaults to “now”
      Calendar calendar = Calendar.getInstance();

      // get a date to represent “today”
      Date today = calendar.getTime();
      System.out.println(“today: ” + today);

      // add one day to the date/calendar
      calendar.add(Calendar.DAY_OF_YEAR, amountOfDateToAdd );

      // now get “tomorrow”
      Date tomorrow = calendar.getTime();

      // print out tomorrow’s date
      System.out.println(“tomorrow: ” + tomorrow);

      Thanks for referring the site, keep following to find more and more updates on this site.

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

%d bloggers like this: