Tuesday, July 26, 2016

How to do Convert the date from one format to another date object in another form Program

how do convert the date from one format to-another date object in another form in java

convert-the-date-from-one-format-to-another-date-object-in-another-form Program

   package com.javasunderesan;

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

public class ConvertDateObject {

 public static void main(String[] args) {
  SimpleDateFormat ddmmyyyy = new SimpleDateFormat("dd/mm/yyyy");
  SimpleDateFormat datetimeFormatyyyymmdd = new SimpleDateFormat("yyyy-mm-dd");
  try {
   ConvertDateObject cdo = new ConvertDateObject();
   String fromDate = "26/05/2016";
   System.out
     .println("Converted Date Time::" + cdo.convertDateType(fromDate, ddmmyyyy, datetimeFormatyyyymmdd));

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private String convertDateType(String date, SimpleDateFormat orgFormat, SimpleDateFormat targetFormat) {
  String formattedDate = null;
  try {
   Date calDate = orgFormat.parse(date);
   formattedDate = targetFormat.format(calDate); // 20120821
  } catch (Exception e) {
   e.printStackTrace();
  }
  return formattedDate;
 }

}

Output:: Converted Date Time::2016-05-26

parse() and format() method used to convert One date object to another date Object

public final String format(Date date)

Formats a Date into a date/time string.
Parameters:
date - the time value to be formatted into a time string.
Returns:
the formatted time string.

Parsing a Date

Parse()

No comments :

Post a Comment