Guest User

Untitled

a guest
Mar 20th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  2. Date d = (Date)formatter.parse(someDate);
  3.  
  4. public class FlexibleDateParser {
  5. private List<ThreadLocal<SimpleDateFormat>> threadLocals = new ArrayList<ThreadLocal<SimpleDateFormat>>();
  6.  
  7. public FlexibleDateParser(List<String> formats, final TimeZone tz){
  8. threadLocals.clear();
  9. for (final String format : formats) {
  10. ThreadLocal<SimpleDateFormat> dateFormatTL = new ThreadLocal<SimpleDateFormat>() {
  11. protected SimpleDateFormat initialValue() {
  12. SimpleDateFormat sdf = new SimpleDateFormat(format);
  13. sdf.setTimeZone(tz);
  14. sdf.setLenient(false);
  15. return sdf;
  16. }
  17. };
  18. threadLocals.add(dateFormatTL);
  19. }
  20. }
  21.  
  22. public Date parseDate(String dateStr) throws ParseException {
  23. for (ThreadLocal<SimpleDateFormat> tl : threadLocals) {
  24. SimpleDateFormat sdf = tl.get();
  25. try {
  26. return sdf.parse(dateStr);
  27. } catch (ParseException e) {
  28. // Ignore and try next date parser
  29. }
  30. }
  31. // All parsers failed
  32. return null;
  33. }
  34. }
  35.  
  36. public static Date parseDate(String str,
  37. String[] parsePatterns)
  38. throws ParseException
  39.  
  40. public Date parseDate(String strDate) throws Exception
  41. {
  42. if (strDate != null && !strDate.isEmpty())
  43. {
  44. SimpleDateFormat[] formats =
  45. new SimpleDateFormat[] {new SimpleDateFormat("MM-dd-yyyy"), new SimpleDateFormat("yyyyMMdd"),
  46. new SimpleDateFormat("MM/dd/yyyy")};
  47.  
  48. Date parsedDate = null;
  49.  
  50. for (int i = 0; i < formats.length; i++)
  51. {
  52. try
  53. {
  54. parsedDate = formats[i].parse(strDate);
  55. return parsedDate;
  56. }
  57. catch (ParseException e)
  58. {
  59. continue;
  60. }
  61. }
  62. }
  63. throw new Exception("Unknown date format: '" + strDate + "'");
  64. }
  65.  
  66. String dateToConvert(String date) {
  67. String strDate = "";
  68. try {
  69. //create SimpleDateFormat object with source string date format
  70. DateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd");
  71.  
  72. //parse the string into Date object
  73. Date d = sdfSource.parse(date);
  74. LocalLog.e(LOG_TAG, d.toString());
  75. //create SimpleDateFormat object with desired date format
  76. SimpleDateFormat sdfDestination = new SimpleDateFormat(AppConstants.UNIVERSAL_DATE_FORMAT);
  77.  
  78. //parse the date into another format
  79. strDate = sdfDestination.format(d);
  80.  
  81. LocalLog.e(LOG_TAG, strDate);
  82.  
  83. } catch (ParseException pe) {
  84. System.out.println("Parse Exception : " + pe);
  85. }
  86. return strDate;
  87. }
  88.  
  89. public String compareDate( PaymentTxnRequest request ) throws ParseException {
  90. Date debitDate= request.getPaymentTxn().getCrValDt();
  91. Date now = new Date();
  92. String response="";
  93. SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
  94. String strCurrDate = sdfDate.format(now);
  95. String strDebitDate = sdfDate.format(debitDate);
  96. System.out.println("Current Date: " + strCurrDate);
  97. Date currentDate = new SimpleDateFormat("dd/MM/yyyy").parse(strCurrDate);
  98. Date txnDate = new SimpleDateFormat("dd/MM/yyyy").parse(strDebitDate);
  99. System.out.println("C -> "+currentDate);
  100. System.out.println("C -> "+txnDate);
  101. if (txnDate!=null){
  102. if (currentDate.equals(txnDate))
  103. {
  104. System.out.println("Valid Txn");
  105. response="valid";
  106. }
  107. if (currentDate.after(txnDate))
  108. {
  109. System.out.println("--> Not Valid TXN Past");
  110. response="notValid";
  111. }
  112. if (currentDateenter code here.before(txnDate)){
  113. System.out.println("Future Valid TXn");
  114. response="future";
  115. }
  116. }
  117. return response;
  118. }
Add Comment
Please, Sign In to add comment