Skip to content

Instantly share code, notes, and snippets.

@javagrails
Last active June 16, 2016 08:50
Show Gist options
  • Save javagrails/5e9aadb5eba4a0f1d82a30c404bc0509 to your computer and use it in GitHub Desktop.
Save javagrails/5e9aadb5eba4a0f1d82a30c404bc0509 to your computer and use it in GitHub Desktop.
Java Custom Date Formatter
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* <b>DateStringFormatter</b>
*
* @author Salman
* Url https://github.com/javagrails
* @ since Created by Salman on 3/22/2016.
* @ Use Example in comments
*/
public class DateStringFormatter {
String givenDateStringPattern = "MM/dd/yyyy";
String wantedDateStringPattern = "dd-MM-yyyy";
public DateStringFormatter() {
}
public DateStringFormatter(String givenDateStringPattern, String wantedDateStringPattern) {
this.givenDateStringPattern = givenDateStringPattern;
this.wantedDateStringPattern = wantedDateStringPattern;
}
private Date getDateFromGivenDateString(String dateString) {
SimpleDateFormat givenDateStringPatternFormatter = new SimpleDateFormat(givenDateStringPattern);
Date date = null;
try {
// (2) give the formatter a String that matches the SimpleDateFormat pattern
System.out.println("givenDateStringPattern : " + givenDateStringPattern + " dateString : " + dateString);
date = givenDateStringPatternFormatter.parse(dateString);
// (3) prints out "Tue Sep 22 00:00:00 EDT 2009"
System.out.println("date : " + date);
} catch (ParseException e) {
// execution will come here if the String that is given
// does not match the expected format.
System.out.println("Exception :: getDateFromGivenDateString.");
e.printStackTrace();
}
return date;
}
public String getFormatedDateString(String dateString) {
SimpleDateFormat wantedDateStringPatternFormatter = new SimpleDateFormat(wantedDateStringPattern);
String wantedDateString = null;
Date date = null;
// new java.text.SimpleDateFormat("dd-MM-yyyy").format(new java.text.SimpleDateFormat("yyyy-MM-dd").parse($P{txtToDate}))
// (1) create a SimpleDateFormat object with the desired format.
// this is the format/pattern we're expecting to receive.
date = this.getDateFromGivenDateString(dateString);
if (date == null) {
date = new Date();
}
//String expectedPattern = "MM/dd/yyyy";
//SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
try {
wantedDateString = wantedDateStringPatternFormatter.format(date);
System.out.println("wantedDateString : " + wantedDateString);
} catch (Exception e) {
// execution will come here if the String that is given
// does not match the expected format.
System.out.println("Exception :: getFormatedDateString.");
e.printStackTrace();
}
return wantedDateString;
}
}
@javagrails
Copy link
Author

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

/**

*/

public class Main {
public static void main(String[] args) {
DateStringFormatter dsfDefault = new DateStringFormatter();
DateStringFormatter dsfCustom = new DateStringFormatter("MM/dd/yyyy","dd-MM-yyyy");

    //  MM/dd/yyyy  ---to----> dd-MM-yyyy
    String userInput1   = "09/22/2009";     
    String userOutput1 = dsfDefault.getFormatedDateString(userInput1);
    System.out.println("\n\nuserInput1 : "+userInput1+" userOutput1 : "+userOutput1);

    String  ddddddddddd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    System.out.println("\n\n ddddddddddd : "+ddddddddddd);




    DateStringFormatter dsfCustom2 = new DateStringFormatter("MM/dd/yyyy","yyyy-MM-dd");        
    //  MM/dd//yyyy  ---to----> yyyy-MM-dd
    String userInput2   = "09/22/2009";     
    String userOutput2 = dsfCustom2.getFormatedDateString(userInput2);
    System.out.println("\n\n userInput2 : "+userInput2+" userOutput2 : "+userOutput2);


    DateStringFormatter dsfCustom3 = new DateStringFormatter("MM/dd/yyyy HH:mm:ss","yyyy-MM-dd HH:mm");     
    //  MM/dd//yyyy  ---to----> yyyy-MM-dd
    String userInput3   = "09/22/2009 11:02:22";        
    String userOutput3 = dsfCustom3.getFormatedDateString(userInput3);
    System.out.println("\n\n userInput3 : "+userInput3+" userOutput3 : "+userOutput3);

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment