Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
- Date d = (Date)formatter.parse(someDate);
- public class FlexibleDateParser {
- private List<ThreadLocal<SimpleDateFormat>> threadLocals = new ArrayList<ThreadLocal<SimpleDateFormat>>();
- public FlexibleDateParser(List<String> formats, final TimeZone tz){
- threadLocals.clear();
- for (final String format : formats) {
- ThreadLocal<SimpleDateFormat> dateFormatTL = new ThreadLocal<SimpleDateFormat>() {
- protected SimpleDateFormat initialValue() {
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- sdf.setTimeZone(tz);
- sdf.setLenient(false);
- return sdf;
- }
- };
- threadLocals.add(dateFormatTL);
- }
- }
- public Date parseDate(String dateStr) throws ParseException {
- for (ThreadLocal<SimpleDateFormat> tl : threadLocals) {
- SimpleDateFormat sdf = tl.get();
- try {
- return sdf.parse(dateStr);
- } catch (ParseException e) {
- // Ignore and try next date parser
- }
- }
- // All parsers failed
- return null;
- }
- }
- public static Date parseDate(String str,
- String[] parsePatterns)
- throws ParseException
- public Date parseDate(String strDate) throws Exception
- {
- if (strDate != null && !strDate.isEmpty())
- {
- SimpleDateFormat[] formats =
- new SimpleDateFormat[] {new SimpleDateFormat("MM-dd-yyyy"), new SimpleDateFormat("yyyyMMdd"),
- new SimpleDateFormat("MM/dd/yyyy")};
- Date parsedDate = null;
- for (int i = 0; i < formats.length; i++)
- {
- try
- {
- parsedDate = formats[i].parse(strDate);
- return parsedDate;
- }
- catch (ParseException e)
- {
- continue;
- }
- }
- }
- throw new Exception("Unknown date format: '" + strDate + "'");
- }
- String dateToConvert(String date) {
- String strDate = "";
- try {
- //create SimpleDateFormat object with source string date format
- DateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd");
- //parse the string into Date object
- Date d = sdfSource.parse(date);
- LocalLog.e(LOG_TAG, d.toString());
- //create SimpleDateFormat object with desired date format
- SimpleDateFormat sdfDestination = new SimpleDateFormat(AppConstants.UNIVERSAL_DATE_FORMAT);
- //parse the date into another format
- strDate = sdfDestination.format(d);
- LocalLog.e(LOG_TAG, strDate);
- } catch (ParseException pe) {
- System.out.println("Parse Exception : " + pe);
- }
- return strDate;
- }
- public String compareDate( PaymentTxnRequest request ) throws ParseException {
- Date debitDate= request.getPaymentTxn().getCrValDt();
- Date now = new Date();
- String response="";
- SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
- String strCurrDate = sdfDate.format(now);
- String strDebitDate = sdfDate.format(debitDate);
- System.out.println("Current Date: " + strCurrDate);
- Date currentDate = new SimpleDateFormat("dd/MM/yyyy").parse(strCurrDate);
- Date txnDate = new SimpleDateFormat("dd/MM/yyyy").parse(strDebitDate);
- System.out.println("C -> "+currentDate);
- System.out.println("C -> "+txnDate);
- if (txnDate!=null){
- if (currentDate.equals(txnDate))
- {
- System.out.println("Valid Txn");
- response="valid";
- }
- if (currentDate.after(txnDate))
- {
- System.out.println("--> Not Valid TXN Past");
- response="notValid";
- }
- if (currentDateenter code here.before(txnDate)){
- System.out.println("Future Valid TXn");
- response="future";
- }
- }
- return response;
- }
Add Comment
Please, Sign In to add comment