Advertisement
hacknik

Android Converting MYSQL DATETIME to ANDROID CALENDER object

Nov 24th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. Converting MYSQL DATETIME to ANDROID CALENDER object
  2.  
  3. private Calendar datetime2Calender(String oldDate)
  4.     {
  5.         // String oldDate = "11/25/2014 12:00:00 PM";
  6.         Calendar cal = null;
  7.         try
  8.         {
  9.             int dd, mm, yy, hh, min, ss;
  10.             String ampm;
  11.             int first, sec;
  12.  
  13.             first = 0;
  14.             sec = oldDate.indexOf('/');
  15.             mm = Integer.parseInt(oldDate.substring(first, sec));
  16.             oldDate = oldDate.substring(sec + 1, oldDate.length());
  17.  
  18.             first = 0;
  19.             sec = oldDate.indexOf('/');
  20.             dd = Integer.parseInt(oldDate.substring(first, sec));
  21.             oldDate = oldDate.substring(sec + 1, oldDate.length());
  22.  
  23.             first = 0;
  24.             sec = oldDate.indexOf(' ');
  25.             yy = Integer.parseInt(oldDate.substring(first, sec));
  26.             oldDate = oldDate.substring(sec + 1, oldDate.length());
  27.  
  28.             first = 0;
  29.             sec = oldDate.indexOf(':');
  30.             hh = Integer.parseInt(oldDate.substring(first, sec));
  31.             oldDate = oldDate.substring(sec + 1, oldDate.length());
  32.  
  33.             first = 0;
  34.             sec = oldDate.indexOf(':');
  35.             min = Integer.parseInt(oldDate.substring(first, sec));
  36.             oldDate = oldDate.substring(sec + 1, oldDate.length());
  37.  
  38.             first = 0;
  39.             sec = oldDate.indexOf(' ');
  40.             ss = Integer.parseInt(oldDate.substring(first, sec));
  41.             oldDate = oldDate.substring(sec + 1, oldDate.length());
  42.  
  43.             first = 0;
  44.             sec = oldDate.length();
  45.             ampm = oldDate.substring(first, sec);
  46.  
  47.             if (ampm.equalsIgnoreCase("pm"))
  48.                 hh = hh + 12;
  49.             else if (hr == 12)
  50.                 hr = 0;
  51.  
  52.             cal = Calendar.getInstance();
  53.  
  54.             cal.set(yy, mm - 1, dd, hh, min, ss);
  55.             return cal;
  56.  
  57.         } catch (Exception e)
  58.         {
  59.         }
  60.  
  61.         return cal;
  62.  
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement