PrefUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.printer.demo.utils;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.text.format.Time;
  5. /**
  6. * SharePreference封装
  7. *
  8. * @author Kevin
  9. *
  10. */
  11. public class PrefUtils {
  12. public static final String PREF_NAME = "config";
  13. public static boolean getBoolean(Context ctx, String key, boolean defaultValue) {
  14. SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  15. return sp.getBoolean(key, defaultValue);
  16. }
  17. public static void setBoolean(Context ctx, String key, boolean value) {
  18. SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  19. sp.edit().putBoolean(key, value).commit();
  20. }
  21. public static String getString(Context ctx, String key, String defaultValue) {
  22. SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  23. return sp.getString(key, defaultValue);
  24. }
  25. public static void setString(Context ctx, String key, String value) {
  26. SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  27. sp.edit().putString(key, value).commit();
  28. }
  29. public static int getInt(Context ctx, String key, int defaultValue) {
  30. SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  31. return sp.getInt(key, defaultValue);
  32. }
  33. public static void setInt(Context ctx, String key, int value) {
  34. SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
  35. sp.edit().putInt(key, value).commit();
  36. }
  37. public static String getSystemTime() {
  38. Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
  39. t.setToNow(); // 取得系统时间。
  40. int year = t.year;
  41. int month = t.month + 1;
  42. int date = t.monthDay;
  43. int hour = t.hour; // 0-23
  44. int minute = t.minute;
  45. int seconds = t.second;
  46. String tag = "AM";
  47. if (hour >= 12) {
  48. tag = "PM";
  49. }
  50. String time = date + "/" + month + "/" + year + " " + hour + ":" + minute + " " + tag;
  51. return time;
  52. }
  53. public static String getSystemTime2() {
  54. Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
  55. t.setToNow(); // 取得系统时间。
  56. int year = t.year;
  57. int month = t.month + 1;
  58. int date = t.monthDay;
  59. int hour = t.hour; // 0-23
  60. int minute = t.minute;
  61. int seconds = t.second;
  62. String tag = "AM";
  63. if (hour >= 12) {
  64. tag = "PM";
  65. }
  66. String time;
  67. if (month < 10) {
  68. if (seconds >= 10) {
  69. time = year + "-0" + month + "-" + date + " " + hour + ":" + minute + ":" + seconds;
  70. } else {
  71. time = year + "-0" + month + "-" + date + " " + hour + ":" + minute + ":0" + seconds;
  72. }
  73. } else {
  74. if (seconds >= 10) {
  75. time = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + seconds;
  76. } else {
  77. time = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":0" + seconds;
  78. }
  79. }
  80. return time;
  81. }
  82. public static String getSystemTime3() {
  83. Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
  84. t.setToNow(); // 取得系统时间。
  85. int year = t.year;
  86. int month = t.month + 1;
  87. int date = t.monthDay;
  88. int hour = t.hour; // 0-23
  89. int minute = t.minute;
  90. int seconds = t.second;
  91. String tag = "AM";
  92. if (hour >= 12) {
  93. tag = "PM";
  94. }
  95. String time;
  96. if (month < 10) {
  97. if (seconds >= 10) {
  98. time = year + "-0" + month + "-" + date;
  99. } else {
  100. time = year + "-0" + month + "-" + date;
  101. }
  102. } else {
  103. if (seconds >= 10) {
  104. time = year + "-" + month + "-" + date;
  105. } else {
  106. time = year + "-" + month + "-" + date;
  107. }
  108. }
  109. return time;
  110. }
  111. }