Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.d4mdp.plesir.util;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.preference.PreferenceManager;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- /**
- * Created by rachmadi on 4/1/16.
- */
- public class Utilities {
- SharedPreferences sp;
- SharedPreferences.Editor editor;
- String value, category, location_id;
- String dateTime;
- public String getUrl(String phpFile){
- // String url = "http://10.0.2.2/tourplanner/" + phpFile;
- String url = "http://hosting.mdp.ac.id/plesir/api/" + phpFile;
- return url;
- }
- public void setCategory(Context context, String category){
- sp = PreferenceManager.getDefaultSharedPreferences(context);
- editor = sp.edit();
- editor.putString("category", category);
- editor.commit();
- }
- public String getCategory(Context context){
- sp = PreferenceManager.getDefaultSharedPreferences(context);
- value = null;
- category = sp.getString("category", value);
- return category;
- }
- public void setLocationId(Context context, String location_id){
- sp = PreferenceManager.getDefaultSharedPreferences(context);
- editor = sp.edit();
- editor.putString("location_id", location_id);
- editor.commit();
- }
- public String getLocationId(Context context){
- sp = PreferenceManager.getDefaultSharedPreferences(context);
- value = null;
- location_id = sp.getString("location_id", value);
- return location_id;
- }
- public String getDuration(String dateTime){
- String datePost = dateTime;
- String dateCurrent = getCurrentDateTime();
- String dtime = "";
- //HH converts hour in 24 hours format (0-23), day calculation
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
- Date d1 = null;
- Date d2 = null;
- try {
- d1 = format.parse(datePost);
- d2 = format.parse(dateCurrent);
- //in milliseconds
- long diff = d2.getTime() - d1.getTime();
- long diffSeconds = diff / 1000 % 60;
- long diffMinutes = diff / (60 * 1000) % 60;
- long diffHours = diff / (60 * 60 * 1000) % 24;
- long diffDays = diff / (24 * 60 * 60 * 1000);
- System.out.print(diffDays + " days, ");
- System.out.print(diffHours + " hours, ");
- System.out.print(diffMinutes + " minutes, ");
- System.out.print(diffSeconds + " seconds.");
- if (diffDays > 0){
- dtime = String.valueOf(diffDays) + " days";
- } else if (diffHours > 0){
- dtime = String.valueOf(diffHours) + " hours";
- } else if (diffMinutes > 0){
- dtime = String.valueOf(diffMinutes) + " mins";
- } else if (diffSeconds >= 0){
- dtime = String.valueOf(diffSeconds) + " secs";
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return dtime;
- }
- public String getCurrentDateTime(){
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
- dateTime = dateFormat.format(new java.util.Date());
- return dateTime;
- }
- public String formatDate(String datetime){
- Locale id = new Locale("en", "EN");
- String pattern = "yyyy-MM-dd";
- SimpleDateFormat sdf = new SimpleDateFormat(
- pattern, id);
- Date myDate = null;
- try {
- myDate = sdf.parse(datetime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- SimpleDateFormat tanggaldf = new SimpleDateFormat("EEEE, dd MMM yyyy", id);
- String finalDate = tanggaldf.format(myDate);
- System.out.println(finalDate);
- return finalDate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement