Advertisement
rachmadi

Utility

Jun 7th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. package com.d4mdp.plesir.util;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.preference.PreferenceManager;
  6.  
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.Locale;
  11.  
  12. /**
  13.  * Created by rachmadi on 4/1/16.
  14.  */
  15. public class Utilities {
  16.  
  17.     SharedPreferences sp;
  18.     SharedPreferences.Editor editor;
  19.     String value, category, location_id;
  20.     String dateTime;
  21.  
  22.     public String getUrl(String phpFile){
  23. //        String url = "http://10.0.2.2/tourplanner/" + phpFile;
  24.         String url = "http://hosting.mdp.ac.id/plesir/api/" + phpFile;
  25.         return url;
  26.     }
  27.  
  28.     public void setCategory(Context context, String category){
  29.         sp = PreferenceManager.getDefaultSharedPreferences(context);
  30.         editor = sp.edit();
  31.         editor.putString("category", category);
  32.         editor.commit();
  33.     }
  34.  
  35.     public String getCategory(Context context){
  36.         sp = PreferenceManager.getDefaultSharedPreferences(context);
  37.         value = null;
  38.         category = sp.getString("category", value);
  39.         return category;
  40.     }
  41.  
  42.     public void setLocationId(Context context, String location_id){
  43.         sp = PreferenceManager.getDefaultSharedPreferences(context);
  44.         editor = sp.edit();
  45.         editor.putString("location_id", location_id);
  46.         editor.commit();
  47.     }
  48.  
  49.     public String getLocationId(Context context){
  50.         sp = PreferenceManager.getDefaultSharedPreferences(context);
  51.         value = null;
  52.         location_id = sp.getString("location_id", value);
  53.         return location_id;
  54.     }
  55.  
  56.     public String getDuration(String dateTime){
  57.  
  58.         String datePost = dateTime;
  59.         String dateCurrent = getCurrentDateTime();
  60.         String dtime = "";
  61.  
  62.         //HH converts hour in 24 hours format (0-23), day calculation
  63.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  64.  
  65.         Date d1 = null;
  66.         Date d2 = null;
  67.  
  68.         try {
  69.             d1 = format.parse(datePost);
  70.             d2 = format.parse(dateCurrent);
  71.  
  72.             //in milliseconds
  73.             long diff = d2.getTime() - d1.getTime();
  74.  
  75.             long diffSeconds = diff / 1000 % 60;
  76.             long diffMinutes = diff / (60 * 1000) % 60;
  77.             long diffHours = diff / (60 * 60 * 1000) % 24;
  78.             long diffDays = diff / (24 * 60 * 60 * 1000);
  79.  
  80.             System.out.print(diffDays + " days, ");
  81.             System.out.print(diffHours + " hours, ");
  82.             System.out.print(diffMinutes + " minutes, ");
  83.             System.out.print(diffSeconds + " seconds.");
  84.  
  85.             if (diffDays > 0){
  86.                 dtime = String.valueOf(diffDays) + " days";
  87.             } else if (diffHours > 0){
  88.                 dtime = String.valueOf(diffHours) + " hours";
  89.             } else if (diffMinutes > 0){
  90.                 dtime = String.valueOf(diffMinutes) + " mins";
  91.             } else if (diffSeconds >= 0){
  92.                 dtime = String.valueOf(diffSeconds) + " secs";
  93.             }
  94.  
  95.         } catch (Exception e) {
  96.             e.printStackTrace();
  97.         }
  98.         return dtime;
  99.     }
  100.  
  101.     public String getCurrentDateTime(){
  102.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  103.         dateTime = dateFormat.format(new java.util.Date());
  104.         return dateTime;
  105.     }
  106.  
  107.     public String formatDate(String datetime){
  108.         Locale id = new Locale("en", "EN");
  109.         String pattern = "yyyy-MM-dd";
  110.         SimpleDateFormat sdf = new SimpleDateFormat(
  111.                 pattern, id);
  112.         Date myDate = null;
  113.         try {
  114.             myDate = sdf.parse(datetime);
  115.  
  116.         } catch (ParseException e) {
  117.             e.printStackTrace();
  118.         }
  119.  
  120.         SimpleDateFormat tanggaldf = new SimpleDateFormat("EEEE, dd MMM yyyy", id);
  121.         String finalDate = tanggaldf.format(myDate);
  122.  
  123.         System.out.println(finalDate);
  124.         return finalDate;
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement