Last active
June 16, 2016 08:50
-
-
Save javagrails/5e9aadb5eba4a0f1d82a30c404bc0509 to your computer and use it in GitHub Desktop.
Java Custom Date Formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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");
}