Thursday, May 3, 2018

Date Format

Change Date Formate


package lk.dialog.ccbs.business;

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

public class Test {
public static void main(String args[]){
String input = "2012/01/20 12:05:10.321";
DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = null;
try {
date = inputFormatter.parse(input);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
String output = outputFormatter.format(date); // Output : 01/20/2012
System.out.println(output);
}

}

Get System Date 

 SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMM" );
String yearmonth = formatter.format( new java.util.Date() );


Get Previous date

Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
String yearmonth = formatter.format(c.getTime());
sysout(yearmonth );

No comments:

Post a Comment