├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── DateTimeUtils ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── thunder413 │ │ └── datetimeutils │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── thunder413 │ │ │ └── datetimeutils │ │ │ ├── DateTimeFormat.java │ │ │ ├── DateTimeStyle.java │ │ │ ├── DateTimeUnits.java │ │ │ └── DateTimeUtils.java │ └── res │ │ ├── values-ar │ │ └── strings.xml │ │ ├── values-en │ │ └── strings.xml │ │ ├── values-es-rES │ │ └── strings.xml │ │ ├── values-fr │ │ └── strings.xml │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── github │ └── thunder413 │ └── datetimeutils │ └── ExampleUnitTest.java ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── thunder413 │ │ └── datetimeutilssample │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── thunder413 │ │ │ └── datetimeutilssample │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── github │ └── thunder413 │ └── datetimeutilssample │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 22 | 34 | 44 | 45 | 46 | 47 | 48 | 49 | 51 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DateTimeUtils/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /DateTimeUtils/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | group = 'com.github.thunder413' 4 | version = '2.1' 5 | 6 | android { 7 | compileSdkVersion 28 8 | 9 | defaultConfig { 10 | minSdkVersion 14 11 | targetSdkVersion 28 12 | versionCode 3 13 | versionName "3.0" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'com.android.support:appcompat-v7:28.0.0' 28 | testImplementation 'junit:junit:4.12' 29 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 30 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 31 | } 32 | -------------------------------------------------------------------------------- /DateTimeUtils/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Crowman\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /DateTimeUtils/src/androidTest/java/com/github/thunder413/datetimeutils/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutils; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.github.thunder413.datetimeutils.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/java/com/github/thunder413/datetimeutils/DateTimeFormat.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutils; 2 | 3 | /** 4 | * DateTimeFormat 5 | * Patterns used to parse given date {@link DateTimeUtils} will use those pattern 6 | * 7 | * @author thunder413 8 | * @version 1.0 9 | */ 10 | @SuppressWarnings("WeakerAccess") 11 | public class DateTimeFormat { 12 | /** 13 | * Typical MySqL/SQL dateTime format with dash as separator 14 | */ 15 | public static final String DATE_TIME_PATTERN_1 = "yyyy-MM-dd HH:mm:ss"; 16 | /** 17 | * Typical MySqL/SQL dateTime format with slash as seperator 18 | */ 19 | public static final String DATE_TIME_PATTERN_2 = "dd/MM/yyyy HH:mm:ss"; 20 | /** 21 | * Typical MySqL/SQL date format with dash as separator 22 | */ 23 | public static final String DATE_PATTERN_1 = "yyyy-MM-dd"; 24 | /** 25 | * Typical MySqL/SQL date format with slash as seperator 26 | */ 27 | public static final String DATE_PATTERN_2 = "dd/MM/yyyy"; 28 | /** 29 | * Time format full 30 | */ 31 | public static final String TIME_PATTERN_1 = "HH:mm:ss"; 32 | } 33 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/java/com/github/thunder413/datetimeutils/DateTimeStyle.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutils; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import java.util.Date; 7 | 8 | /** 9 | * DateTimeStyle 10 | * 11 | * Defined style for parsing date into string used by {@link DateTimeUtils#formatWithStyle(Date, DateTimeStyle)} 12 | * and also {@link DateTimeUtils#getTimeAgo(Context, Date, DateTimeStyle)} 13 | * 14 | * @author thunder413 15 | * @version 1.0 16 | */ 17 | public enum DateTimeStyle { 18 | /** 19 | * Style full e.g Tuesday, June 13, 2017 20 | */ 21 | FULL, 22 | /** 23 | * Style long e.g June 13, 2017 24 | */ 25 | LONG, 26 | /** 27 | * Style medium e.g Jun 13, 2017 28 | */ 29 | MEDIUM, 30 | /** 31 | * Style short e.g 06/13/17 32 | */ 33 | SHORT, 34 | /** 35 | * Style for ago time e.g 3h ago 36 | */ 37 | AGO_SHORT_STRING, 38 | /** 39 | * Style for ago time e.g 3 hours ago 40 | */ 41 | AGO_FULL_STRING 42 | } 43 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/java/com/github/thunder413/datetimeutils/DateTimeUnits.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutils; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * DateTimeUnits 7 | * Define units used by {@link DateTimeUtils#getDateDiff(Date, Date, DateTimeUnits)} 8 | * and also {@link DateTimeUtils#formatDate(long, DateTimeUnits)} 9 | * 10 | * @author thunder413 11 | * @version 1.0 12 | */ 13 | @SuppressWarnings("WeakerAccess") 14 | public enum DateTimeUnits { 15 | /** 16 | * Days 17 | */ 18 | DAYS, 19 | /** 20 | * Hours 21 | */ 22 | HOURS, 23 | /** 24 | * Minutes 25 | */ 26 | MINUTES, 27 | /** 28 | * Seconds 29 | */ 30 | SECONDS, 31 | /** 32 | * Milliseconds 33 | */ 34 | MILLISECONDS, 35 | } 36 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/java/com/github/thunder413/datetimeutils/DateTimeUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutils; 2 | 3 | 4 | import android.content.Context; 5 | import android.text.format.DateUtils; 6 | import android.util.Log; 7 | 8 | import java.text.ParseException; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Calendar; 11 | import java.util.Date; 12 | import java.util.Locale; 13 | import java.util.TimeZone; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | /** 17 | * DateTimeUtils 18 | * This class contains a bunch of function that can manipulate 19 | * Date object or Date String to achieve certain operations 20 | * e.g : Time difference, Time Ago, Date formatting 21 | * 22 | * @author thunder413 23 | * @version 1.5 24 | */ 25 | @SuppressWarnings("WeakerAccess") 26 | public class DateTimeUtils { 27 | /** 28 | * LOG TAG 29 | */ 30 | private static final String LOG_TAG = "DateTimeUtils"; 31 | 32 | /** 33 | * Debug mode 34 | */ 35 | private static boolean debug; 36 | 37 | /** 38 | * Time zone 39 | */ 40 | private static String timeZone = "UTC"; 41 | 42 | /** 43 | * Enable / Disable 44 | * @param state Debug state 45 | */ 46 | public static void setDebug(boolean state){ 47 | debug = state; 48 | } 49 | 50 | /** 51 | * Set TimeZone 52 | * @param zone TimeZone 53 | */ 54 | public static void setTimeZone(String zone){ 55 | timeZone = zone; 56 | } 57 | 58 | /** 59 | * Get Date or DateTime formatting pattern 60 | * 61 | * @param dateString Date String 62 | * @return Format Pattern 63 | */ 64 | private static String getDatePattern(String dateString) { 65 | if (isDateTime(dateString)) { 66 | return (dateString.contains("/")) ? DateTimeFormat.DATE_TIME_PATTERN_2 : DateTimeFormat.DATE_TIME_PATTERN_1; 67 | } else { 68 | return (dateString.contains("/")) ? DateTimeFormat.DATE_PATTERN_2 : DateTimeFormat.DATE_PATTERN_1; 69 | } 70 | } 71 | 72 | /** 73 | * Convert a Java Date object to String 74 | * 75 | * @param date Date Object 76 | * @param locale Locale 77 | * @return Date Object string representation 78 | */ 79 | public static String formatDate(Date date, Locale locale) { 80 | if(date == null && debug){ 81 | Log.e(LOG_TAG,"formatDate >> Supplied date is null"); 82 | } 83 | SimpleDateFormat iso8601Format = new SimpleDateFormat(DateTimeFormat.DATE_TIME_PATTERN_1, locale); 84 | iso8601Format.setTimeZone(TimeZone.getTimeZone(timeZone)); 85 | if(debug) { 86 | Log.d(LOG_TAG,"formatDate >> Formatting using "+iso8601Format.getTimeZone().getDisplayName()+" | "+iso8601Format.getTimeZone().getID()); 87 | } 88 | return iso8601Format.format(date); 89 | } 90 | 91 | /** 92 | * Convert a date string to Java Date Object 93 | * 94 | * @param dateString Date String 95 | * @param locale Locale 96 | * @return Java Date Object 97 | */ 98 | public static Date formatDate(String dateString, Locale locale) { 99 | SimpleDateFormat iso8601Format = new SimpleDateFormat(getDatePattern(dateString), locale); 100 | iso8601Format.setTimeZone(TimeZone.getTimeZone(timeZone)); 101 | Date date = null; 102 | if (dateString != null) { 103 | try { 104 | date = iso8601Format.parse(dateString.trim()); 105 | } catch (ParseException e) { 106 | if(debug) { 107 | Log.e(LOG_TAG,"formatDate >> Fail to parse supplied date string >> "+dateString); 108 | e.printStackTrace(); 109 | } 110 | } 111 | } 112 | return date; 113 | } 114 | /** 115 | * Convert a Java Date object to String 116 | * 117 | * @param date Date Object 118 | * @return Date Object string representation 119 | */ 120 | public static String formatDate(Date date) { 121 | return formatDate(date, Locale.getDefault()); 122 | } 123 | /** 124 | * Convert a date string to Java Date Object 125 | * 126 | * @param date Date String 127 | * @return Java Date Object 128 | */ 129 | public static Date formatDate(String date) { 130 | return formatDate(date, Locale.getDefault()); 131 | } 132 | 133 | /** 134 | * Convert a timeStamp into a date object 135 | * 136 | * @param timeStamp TimeStamp 137 | * @param units Witch unit is whether seconds or milliseconds 138 | * @see DateTimeUnits#SECONDS 139 | * @see DateTimeUnits#MILLISECONDS 140 | * @return Date object 141 | */ 142 | public static Date formatDate(long timeStamp, DateTimeUnits units){ 143 | if(units.equals(DateTimeUnits.SECONDS)) 144 | return new Date(timeStamp*1000L); 145 | else 146 | return new Date(timeStamp); 147 | } 148 | /** 149 | * Convert a timeStamp into a date considering given timeStamp in milliseconds 150 | * 151 | * @see DateTimeUnits#MILLISECONDS 152 | * @param timeStamp TimeStamp 153 | * @return Date object 154 | */ 155 | public static Date formatDate(long timeStamp){ 156 | return formatDate(timeStamp,DateTimeUnits.MILLISECONDS); 157 | } 158 | /** 159 | * Format date using a given pattern 160 | * and apply supplied locale 161 | * 162 | * @param date Date Object 163 | * @param pattern Pattern 164 | * @param locale Locale 165 | * @return Formatted date 166 | */ 167 | public static String formatWithPattern(Date date, String pattern, Locale locale) { 168 | if(date == null && debug) { 169 | Log.e(LOG_TAG,"FormatWithPattern >> Supplied date is null"); 170 | } 171 | SimpleDateFormat iso8601Format = new SimpleDateFormat(pattern, locale); 172 | iso8601Format.setTimeZone(TimeZone.getTimeZone(timeZone)); 173 | return iso8601Format.format(date); 174 | } 175 | 176 | /** 177 | * Format date using a given pattern 178 | * and apply supplied locale 179 | * 180 | * @param date Date String 181 | * @param pattern Pattern 182 | * @param locale Locale 183 | * @return Formatted date 184 | */ 185 | public static String formatWithPattern(String date, String pattern, Locale locale) { 186 | return formatWithPattern(formatDate(date),pattern,locale); 187 | } 188 | /** 189 | * Format date using a given pattern 190 | * apply default locale 191 | * 192 | * @param date Date Object 193 | * @param pattern Pattern 194 | * 195 | * @return Formatted date 196 | */ 197 | public static String formatWithPattern(Date date, String pattern) { 198 | return formatWithPattern(date,pattern,Locale.getDefault()); 199 | } 200 | /** 201 | * Format date using a given pattern 202 | * apply default locale 203 | * @param date Date String 204 | * @param pattern Pattern 205 | * 206 | * @return Formatted date 207 | */ 208 | public static String formatWithPattern(String date, String pattern) { 209 | return formatWithPattern(date,pattern,Locale.getDefault()); 210 | } 211 | /** 212 | * Build a pattern for given style 213 | * @param style DateTimeStyle 214 | * @return Pattern 215 | */ 216 | private static String getPatternForStyle(DateTimeStyle style) { 217 | String pattern; 218 | if(style.equals(DateTimeStyle.LONG)){ 219 | pattern = "MMMM dd, yyyy"; 220 | } else if(style.equals(DateTimeStyle.MEDIUM)) { 221 | pattern = "MMM dd, yyyy"; 222 | } else if(style.equals(DateTimeStyle.SHORT)) { 223 | pattern = "MM/dd/yy"; 224 | } else { 225 | pattern = "EEEE, MMMM dd, yyyy"; 226 | } 227 | return pattern; 228 | } 229 | /** 230 | * Get localized date string 231 | * 232 | * @param date Date string 233 | * @return Formatted localized date string 234 | */ 235 | public static String formatWithStyle(Date date, DateTimeStyle style, Locale locale) { 236 | if(date == null && debug) { 237 | Log.e(LOG_TAG,"FormatWithPattern >> Supplied date is null"); 238 | } 239 | return formatWithPattern(date,getPatternForStyle(style),locale); 240 | } 241 | /** 242 | * Get localized date string (Using default locale) 243 | * 244 | * @param date Date string 245 | * @return Formatted localized date string 246 | */ 247 | public static String formatWithStyle(String date, DateTimeStyle style, Locale locale) { 248 | return formatWithStyle(formatDate(date),style,locale); 249 | } 250 | /** 251 | * Get localized date string (Using default locale) 252 | * 253 | * @param date Date string 254 | * @return Formatted localized date string 255 | */ 256 | public static String formatWithStyle(Date date, DateTimeStyle style) { 257 | return formatWithStyle(date,style,Locale.getDefault()); 258 | } 259 | /** 260 | * Get localized date string (Using default locale) 261 | * 262 | * @param date Date string 263 | * @return Formatted localized date string 264 | */ 265 | public static String formatWithStyle(String date, DateTimeStyle style) { 266 | return formatWithStyle(date,style,Locale.getDefault()); 267 | } 268 | 269 | /** 270 | * Extract time from date without seconds 271 | * @see DateTimeFormat#TIME_PATTERN_1 272 | * @param date Date object 273 | * @return Time String 274 | */ 275 | public static String formatTime(Date date, boolean forceShowHours){ 276 | SimpleDateFormat iso8601Format = new SimpleDateFormat(DateTimeFormat.TIME_PATTERN_1, Locale.getDefault()); 277 | iso8601Format.setTimeZone(TimeZone.getTimeZone(timeZone)); 278 | String time = iso8601Format.format(date); 279 | String[] hhmmss = time.split(":"); 280 | int hours = Integer.parseInt(hhmmss[0]); 281 | int minutes = Integer.parseInt(hhmmss[1]); 282 | int seconds = Integer.parseInt(hhmmss[2]); 283 | return (hours == 0 && !forceShowHours ? "" : hours < 10 ? "0" + hours +":" : 284 | hours +":") + 285 | (minutes == 0 ? "00" : minutes < 10 ? "0" + minutes : 286 | String.valueOf(minutes))+":" 287 | + (seconds == 0 ? "00" : seconds < 10 ? "0" + seconds : String.valueOf(seconds)); 288 | } 289 | /** 290 | * Extract time from date without seconds 291 | * @param date Date object 292 | * @return Time string 293 | */ 294 | public static String formatTime(String date, boolean forceShowHours){ 295 | return formatTime(formatDate(date),forceShowHours); 296 | } 297 | /** 298 | * Extract time from date without seconds 299 | * @param date Date object 300 | * @return Time string 301 | */ 302 | public static String formatTime(Date date){ 303 | return formatTime(date,false); 304 | } 305 | /** 306 | * Extract time from date without seconds 307 | * @param date Date object 308 | * @return Time string 309 | */ 310 | public static String formatTime(String date){ 311 | return formatTime(date,false); 312 | } 313 | 314 | /** 315 | * Convert millis to human readable time 316 | * 317 | * @param millis TimeStamp 318 | * 319 | * @return Time String 320 | */ 321 | public static String millisToTime(long millis){ 322 | long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) 323 | - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)); 324 | long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) 325 | - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)); 326 | long hours = TimeUnit.MILLISECONDS.toHours(millis); 327 | 328 | return (hours == 0 ? "" : hours < 10 ? "0" + hours +":" : 329 | hours +":") + 330 | (minutes == 0 ? "00" : minutes < 10 ? "0" + minutes : 331 | String.valueOf(minutes)) + ":" 332 | + (seconds == 0 ? "00" : seconds < 10 ? "0" + seconds 333 | : String.valueOf(seconds)); 334 | 335 | } 336 | /** 337 | * Convert millis to human readable time 338 | * 339 | * @param time Time string 340 | * @return Time String 341 | */ 342 | public static long timeToMillis(String time) { 343 | String[] hhmmss = time.split(":"); 344 | int hours = 0; 345 | int minutes; 346 | int seconds; 347 | if(hhmmss.length == 3) { 348 | hours = Integer.parseInt(hhmmss[0]); 349 | minutes = Integer.parseInt(hhmmss[1]); 350 | seconds = Integer.parseInt(hhmmss[2]); 351 | } else { 352 | minutes = Integer.parseInt(hhmmss[0]); 353 | seconds = Integer.parseInt(hhmmss[1]); 354 | } 355 | return (((hours * 60)+(minutes * 60) + seconds) * 1000); 356 | } 357 | /** 358 | * Tell whether or not a given string represent a date time string or a simple date 359 | * 360 | * @param dateString Date String 361 | * @return True if given string is a date time False otherwise 362 | */ 363 | public static boolean isDateTime(String dateString) { 364 | return (dateString != null) && (dateString.trim().split(" ").length > 1); 365 | } 366 | /** 367 | * Tell whether or not a given date is yesterday 368 | * @param date Date Object 369 | * @return True if the date is yesterday False otherwise 370 | */ 371 | public static boolean isYesterday(Date date){ 372 | // Check if yesterday 373 | Calendar c1 = Calendar.getInstance(); // today 374 | c1.add(Calendar.DAY_OF_YEAR, -1); // yesterday 375 | Calendar c2 = Calendar.getInstance(); 376 | c2.setTime(date); // 377 | return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) 378 | && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR); 379 | } 380 | /** 381 | * Tell whether or not a given date is yesterday 382 | * @param dateString Date String 383 | * @return True if the date is yesterday False otherwise 384 | */ 385 | public static boolean isYesterday(String dateString){ 386 | return isYesterday(formatDate(dateString)); 387 | } 388 | 389 | /** 390 | * Tell whether or not a given date is today date 391 | * @param date Date object 392 | * @return True if date is today False otherwise 393 | */ 394 | public static boolean isToday(Date date){ 395 | return DateUtils.isToday(date.getTime()); 396 | } 397 | 398 | /** 399 | * Tell whether or not a given date is today date 400 | * @param dateString Date string 401 | * @return True if date is today False otherwise 402 | */ 403 | public static boolean isToday(String dateString){ 404 | return isToday(formatDate(dateString)); 405 | } 406 | 407 | /** 408 | * Get Previous month from a given date 409 | * @param date Date start 410 | * @return Date of the previous month 411 | */ 412 | public static Date getPreviousMonthDate(Date date) { 413 | Calendar c = Calendar.getInstance(); 414 | c.setTime(date); // 415 | c.add(Calendar.MONTH, -1); 416 | return c.getTime(); 417 | } 418 | 419 | /** 420 | * Get Previous month from a given date 421 | * @param date Date start 422 | * @return Date of the previous month 423 | */ 424 | public static Date getPreviousMonthDate(String date) { 425 | return getPreviousMonthDate(formatDate(date)); 426 | } 427 | 428 | /** 429 | * Get Next month from a given date 430 | * @param date Date start 431 | * @return Date of the previous month 432 | */ 433 | public static Date getNextMonthDate(Date date) { 434 | Calendar c = Calendar.getInstance(); 435 | c.setTime(date); // 436 | c.add(Calendar.MONTH, 1); 437 | return c.getTime(); 438 | } 439 | /** 440 | * Get Previous month from a given date 441 | * @param date String Date start 442 | * @return Date of the previous month 443 | */ 444 | public static Date getNextMonthDate(String date) { 445 | return getNextMonthDate(formatDate(date)); 446 | } 447 | 448 | /** 449 | * Get Previous week date 450 | * @param date Date Object 451 | * @param dayOfTheWeek Day Of the week 452 | * @return Date 453 | */ 454 | public static Date getPreviousWeekDate(Date date, int dayOfTheWeek) { 455 | Calendar c = Calendar.getInstance(); 456 | c.setTime(date); 457 | c.setFirstDayOfWeek(dayOfTheWeek); 458 | c.set(Calendar.DAY_OF_WEEK, dayOfTheWeek); 459 | c.add(Calendar.DATE, -7); 460 | return c.getTime(); 461 | } 462 | 463 | /** 464 | * Get Previous week date 465 | * @param date Date String 466 | * @param dayOfTheWeek Day Of the week 467 | * @return Date 468 | */ 469 | public static Date getPreviousWeekDate(String date, int dayOfTheWeek) { 470 | return getPreviousWeekDate(formatDate(date),dayOfTheWeek); 471 | } 472 | 473 | /** 474 | * Get Next week date 475 | * @param date Date Object 476 | * @param dayOfTheWeek Day Of the week 477 | * @return Date 478 | */ 479 | public static Date getNextWeekDate(Date date, int dayOfTheWeek) { 480 | Calendar c = Calendar.getInstance(); 481 | c.setTime(date); 482 | c.setFirstDayOfWeek(dayOfTheWeek); 483 | c.set(Calendar.DAY_OF_WEEK, dayOfTheWeek); 484 | c.add(Calendar.DATE, 7); 485 | return c.getTime(); 486 | } 487 | 488 | /** 489 | * Get Next week date 490 | * @param date Date Object 491 | * @return Date 492 | */ 493 | public static Date getNextWeekDate(Date date) { 494 | return getNextWeekDate(date,Calendar.MONDAY); 495 | } 496 | /** 497 | * Get Next week date 498 | * @param date Date Object 499 | * @return Date 500 | */ 501 | public static Date getNextWeekDate(String date) { 502 | return getNextWeekDate(formatDate(date)); 503 | } 504 | /** 505 | * Get Next week date 506 | * @param date Date Object 507 | * @param dayOfTheWeek Day Of the week 508 | * @return Date 509 | */ 510 | public static Date getNextWeekDate(String date, int dayOfTheWeek) { 511 | return getNextWeekDate(formatDate(date),dayOfTheWeek); 512 | } 513 | /** 514 | * Get difference between two dates 515 | * 516 | * @param nowDate Current date 517 | * @param oldDate Date to compare 518 | * @param dateDiff Difference Unit 519 | * @return Difference 520 | */ 521 | public static int getDateDiff(Date nowDate, Date oldDate, DateTimeUnits dateDiff) { 522 | long diffInMs = nowDate.getTime() - oldDate.getTime(); 523 | int days = (int) TimeUnit.MILLISECONDS.toDays(diffInMs); 524 | int hours = (int) (TimeUnit.MILLISECONDS.toHours(diffInMs) - TimeUnit.DAYS.toHours(days)); 525 | int minutes = (int) (TimeUnit.MILLISECONDS.toMinutes(diffInMs) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(diffInMs))); 526 | int seconds = (int) TimeUnit.MILLISECONDS.toSeconds(diffInMs); 527 | switch (dateDiff) { 528 | case DAYS: 529 | return days; 530 | case SECONDS: 531 | return seconds; 532 | case MINUTES: 533 | return minutes; 534 | case HOURS: 535 | return hours; 536 | case MILLISECONDS: 537 | default: 538 | return (int) diffInMs; 539 | } 540 | } 541 | /** 542 | * Get difference between two dates 543 | * 544 | * @param nowDate Current date 545 | * @param oldDate Date to compare 546 | * @param dateDiff Difference Unit 547 | * @return Difference 548 | */ 549 | public static int getDateDiff(String nowDate, Date oldDate, DateTimeUnits dateDiff) { 550 | return getDateDiff(formatDate(nowDate),oldDate,dateDiff); 551 | } 552 | /** 553 | * Get difference between two dates 554 | * 555 | * @param nowDate Current date 556 | * @param oldDate Date to compare 557 | * @param dateDiff Difference Unit 558 | * @return Difference 559 | */ 560 | public static int getDateDiff(Date nowDate, String oldDate, DateTimeUnits dateDiff) { 561 | return getDateDiff(nowDate,formatDate(oldDate),dateDiff); 562 | } 563 | /** 564 | * Get difference between two dates 565 | * 566 | * @param nowDate Current date 567 | * @param oldDate Date to compare 568 | * @param dateDiff Difference Unit 569 | * @return Difference 570 | */ 571 | public static int getDateDiff(String nowDate, String oldDate, DateTimeUnits dateDiff) { 572 | return getDateDiff(nowDate,formatDate(oldDate),dateDiff); 573 | } 574 | /** 575 | * Get time ago of given date 576 | * 577 | * @param context Context 578 | * @param date Date object 579 | * @param style DateTimeStyle 580 | * @return Time ago string 581 | */ 582 | public static String getTimeAgo(Context context, Date date, DateTimeStyle style) { 583 | double seconds = getDateDiff(new Date(), date, DateTimeUnits.SECONDS); 584 | double minutes = seconds / 60; 585 | double hours = minutes / 60; 586 | double days = hours / 24; 587 | String phrase; 588 | String s; 589 | if (seconds <= 0) { 590 | phrase = context.getString(R.string.time_ago_now); 591 | } else if (seconds < 45) { 592 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_seconds): 593 | context.getString(R.string.time_ago_seconds); 594 | phrase = String.format(s, Math.round(seconds)); 595 | } else if (seconds < 90) { 596 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_minute): 597 | context.getString(R.string.time_ago_minute); 598 | phrase = String.format(s,1); 599 | } else if (minutes < 45) { 600 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_minutes): 601 | context.getString(R.string.time_ago_minutes); 602 | phrase = String.format(s, Math.round(minutes)); 603 | } else if (minutes < 90) { 604 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_hour): 605 | context.getString(R.string.time_ago_hour); 606 | phrase = String.format(s, 1); 607 | } else if (hours < 24) { 608 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_hours): 609 | context.getString(R.string.time_ago_hours); 610 | phrase = String.format(s,Math.round(hours)); 611 | } else if (hours < 42) { 612 | if (isYesterday(date)) { 613 | phrase = context.getString(R.string.time_ago_yesterday_at, formatTime(date)); 614 | } else { 615 | phrase = formatWithStyle(date,style.equals(DateTimeStyle.AGO_FULL_STRING)? 616 | DateTimeStyle.FULL:DateTimeStyle.SHORT); 617 | } 618 | } else if (days < 30) { 619 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_days): 620 | context.getString(R.string.time_ago_days); 621 | phrase = String.format(s, Math.round(days)); 622 | } else if (days < 45) { 623 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_month): 624 | context.getString(R.string.time_ago_month); 625 | phrase = String.format(s, 1); 626 | } else if (days < 365) { 627 | s = style.equals(DateTimeStyle.AGO_FULL_STRING) ? context.getString(R.string.time_ago_full_months): 628 | context.getString(R.string.time_ago_months); 629 | phrase = String.format(s, Math.round(days / 30)); 630 | } else { 631 | phrase = formatWithStyle(date,style.equals(DateTimeStyle.AGO_FULL_STRING)? 632 | DateTimeStyle.FULL:DateTimeStyle.SHORT); 633 | } 634 | return phrase; 635 | } 636 | /** 637 | * Get time ago of given date 638 | * 639 | * @param context Context 640 | * @param dateString Representing a date time string 641 | * @return Time ago string 642 | */ 643 | public static String getTimeAgo(Context context, String dateString) { 644 | return getTimeAgo(context, formatDate(dateString),DateTimeStyle.AGO_FULL_STRING); 645 | } 646 | /** 647 | * Get time ago of given date 648 | * 649 | * @param context Context 650 | * @param date Representing a date time string 651 | * @return Time ago string 652 | */ 653 | public static String getTimeAgo(Context context, Date date) { 654 | return getTimeAgo(context, date,DateTimeStyle.AGO_FULL_STRING); 655 | } 656 | 657 | } -------------------------------------------------------------------------------- /DateTimeUtils/src/main/res/values-ar/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/res/values-en/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/res/values-es-rES/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/res/values-fr/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | à L\'instant 4 | Il y\'a %1$d seconds 5 | Il y\'a %1$d minute 6 | Il y\'a %1$d minutes 7 | Il y\'a %1$d heure 8 | Il y\'a %1$d heures 9 | Hier à %1$s 10 | Il y\'a %1$s jours 11 | Il y\'a %1$s mois 12 | Il y\'a %1$s mois 13 | Il y\'a %1$s sec 14 | Il y\'a %1$s min 15 | Il y\'a %1$s min 16 | Il y\'a %1$s h 17 | Il y\'a %1$s h 18 | Hier à %1$s 19 | y\'a %1$s js 20 | y\'a %1$s mois 21 | y\'a %1$s mois 22 | -------------------------------------------------------------------------------- /DateTimeUtils/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | few moments ago 3 | %1$d seconds ago 4 | %1$d minute ago 5 | %1$d minutes ago 6 | %1$d hour ago 7 | %1$d hours ago 8 | Yesterday at %1$s 9 | %1$d days ago 10 | %1$d month ago 11 | %1$d months ago 12 | 13 | %1$d sec ago 14 | %1$d min ago 15 | %1$d min ago 16 | %1$d h ago 17 | %1$d h ago 18 | Yesterday at %1$s 19 | %1$d d ago 20 | %1$d m ago 21 | %1$d m ago 22 | 23 | -------------------------------------------------------------------------------- /DateTimeUtils/src/test/java/com/github/thunder413/datetimeutils/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutils; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Thunder413 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DateTimeUtils 2 | 3 | This library is a package of functions that let you manipulate objects and or java date string. it combine the most common functions used when managing dates under android, such as converting a mysql /sqlLite date to a Date object and vis-versa etc. 4 | 5 | This library is available under the [MIT License](http://www.opensource.org/licenses/mit-license.php). 6 | 7 | 8 | 9 | ## Usage 10 | 11 | The DateTimeUtils library is available from [JitPack](https://jitpack.io/#thunder413/NetRequest/1.2). 12 | 13 | First add JitPack dependency line in your project `build.gradle` file: 14 | 15 | ```xml 16 | allprojects { 17 | repositories { 18 | ... 19 | maven { url 'https://jitpack.io' } 20 | } 21 | } 22 | ``` 23 | 24 | And then simply add the following line to the `dependencies` section of your app module `build.gradle` file: 25 | 26 | ```groovy 27 | implementation 'com.github.thunder413:DateTimeUtils:3.0' 28 | ``` 29 | 30 | Javadocs are available [here](http://https://github.com/thunder413/DateTimeUtils/apidocs/index.html). 31 | 32 | ## Examples 33 | 34 | ### setTimeZone 35 | 36 | ``setTimeZone`` allow you to define your time zone by default it's ``UTC`` 37 | 38 | ```java 39 | DateTimeUtils.setTimeZone("UTC"); 40 | ``` 41 | ### formatDate 42 | 43 | ``formatDate`` is a method that allow you to convert ``date object`` to ``string`` or ``timeStamp`` to date and vice-versa. 44 | 45 | #### Date string to Date object 46 | 47 | ```java 48 | // MySQL/SQLite dateTime example 49 | Date date = DateTimeUtils.formatDate("2017-06-13 04:14:49"); 50 | // Or also with / separator 51 | Date date = DateTimeUtils.formatDate("2017/06/13 04:14:49"); 52 | // MySQL/SQLite date example 53 | Date date = DateTimeUtils.formatDate("2017-06-13"); 54 | // Or also with / separator 55 | Date date = DateTimeUtils.formatDate("2017/06/13"); 56 | ``` 57 | #### Date object to date string MySQL/SQLite 58 | 59 | ```java 60 | String date = DateTimeUtils.formatDate(new Date()); 61 | ``` 62 | 63 | #### timeStamp to Date object 64 | 65 | By default it will considere given timeStamp in milliseconds but in case you did retrieve the timeStamp from server wich usually will be in seconds supply ``DateTimeUnits.SECONDS`` to tell the fonction about 66 | 67 | ```java 68 | // Using milliseconds 69 | Date date = DateTimeUtils.formatDate(1497399731000); 70 | // Using seconds (Server timeStamp) 71 | Date date = DateTimeUtils.formatDate(1497399731,DateTimeUnits.SECONDS); 72 | ``` 73 | 74 | ### formatWithStyle 75 | 76 | ``formatWithStyle`` allow to parse date into localized format using most common style 77 | 78 | #### Date object to localized date 79 | 80 | ```java 81 | DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.FULL); // Tuesday, June 13, 2017 82 | DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.LONG); // June 13, 2017 83 | DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.MEDIUM); // Jun 13, 2017 84 | DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.SHORT); // 06/13/17 85 | ``` 86 | 87 | #### Date string to localized date 88 | 89 | ```java 90 | DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.FULL); // Tuesday, June 13, 2017 91 | DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.LONG); // June 13, 2017 92 | DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.MEDIUM); // Jun 13, 2017 93 | DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.SHORT); // 06/13/17 94 | ``` 95 | 96 | ### formatWithPattern 97 | 98 | ``formatWithPattern`` allow to define your own parse pattern following ``SimpleDateFormat`` scheme 99 | 100 | #### Date string as source 101 | 102 | ```java 103 | DateTimeUtils.formatWithPattern("2017-06-13", "EEEE, MMMM dd, yyyy"); // Tuesday, June 13, 2017 104 | ``` 105 | 106 | #### Date object as source 107 | 108 | ```java 109 | DateTimeUtils.formatWithPattern(new Date(), "EEEE, MMMM dd, yyyy"); // Tuesday, June 13, 2017 110 | ``` 111 | 112 | ### isToday 113 | 114 | ``isToday`` Tell whether or not a given date is today date 115 | 116 | ```java 117 | // Date object as source 118 | boolean state = DateTimeUtils.isToday(new Date()); 119 | // Date String as source 120 | boolean state = DateTimeUtils.isToday("2017-06-15 04:14:49"); 121 | ``` 122 | 123 | ### isYesterday 124 | 125 | ``isYesterday`` Tell whether or not a given date is yesterday date 126 | 127 | ```java 128 | // Date object as source 129 | boolean state = DateTimeUtils.isYesterday(new Date()); 130 | // Date String as source 131 | boolean state = DateTimeUtils.isYestrday("2017-06-15 04:14:49"); 132 | ``` 133 | ### Get Previous next Week 134 | 135 | ``getPreviousWeekDate/getNextWeekDate`` Return the next or a previous week date from a given date it also allow you to set the day of the week by using Calendar Constant 136 | 137 | ```java 138 | // Date object as source 139 | Date date = DateTimeUtils.getPreviousWeekDate(new Date(), Calendar.MONDAY); 140 | // Date String as source 141 | Date date = DateTimeUtils.getNextWeekDate("2017-06-15 04:14:49",Calendar.SUNDAY); 142 | 143 | ``` 144 | ### Get Previous next month 145 | 146 | ``getPreviousMonthDate/getNextMonthDate`` Return the next or a previous month date from a given date 147 | 148 | ```java 149 | // Date object as source 150 | Date date = DateTimeUtils.getNextMonthDate(new Date()); 151 | // Date String as source 152 | Date date = DateTimeUtils.getPreviousMonthDate("2017-06-15 04:14:49"); 153 | 154 | ``` 155 | ### getDateDiff 156 | 157 | ``getDateDiff`` give you the difference between two date in days, hours, minutes, seconds or milliseconds ``DateTimeUnits`` 158 | 159 | ```java 160 | // Dates can be date object or date string 161 | Date date = new Date(); 162 | String date2 = "2017-06-13 04:14:49"; 163 | // Get difference in milliseconds 164 | int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.MILLISECONDS); 165 | // Get difference in seconds 166 | int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.SECONDS); 167 | // Get difference in minutes 168 | int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.MINUTES); 169 | // Get difference in hours 170 | int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.HOURS); 171 | // Get difference in days 172 | int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.DAYS); 173 | ``` 174 | 175 | ### getTimeAgo 176 | 177 | ``getTimeAgo`` give ou the elapsed time since a given date, it also offer two print mode the full and short strings ``eg . 3 hours ago | 3h ago`` the strings are localized but at the moment only FR and EN language are available. If you need your langage to be add just let me know :) 178 | 179 | ```java 180 | String timeAgo = DateTimeUtils.getTimeAgo(context,new Date()); // Full string style will be used 181 | // Short string style 182 | String timeAgo = DateTimeUtils.getTimeAgo(context,"new Date()",DateTimeStyle.AGO_SHORT_STRING ); 183 | ``` 184 | 185 | ### formatTime 186 | 187 | ``formatTime`` allow you to extract time from date by default it wont show the hours if equal to ``0`` but you can supply ``forceShowHours`` parameter to force hours display 188 | 189 | ```java 190 | String time = DateTimeUtils.formatTime(new Date()); // 14:49 if hours equals 0 or 04:14:09 if hours witch is wrong when use it on time rather than a duration 191 | // Solution >> force hours display 192 | String time = DateTimeUtils.formatTime(new Date(),true); 193 | // And you can also supplie a date string 194 | String time = DateTimeUtils.formatTime("2017-06-13 04:14:49"); // 04:14:49 195 | ``` 196 | 197 | ### millisToTime 198 | 199 | ``millisToTime`` is usefull when your dealing with duration and want to display for example player duration or current playback position into human readable value. 200 | 201 | ```java 202 | String time = DateTimeUtils.millisToTime(2515); // It take millis as an argument not seconds 203 | ``` 204 | 205 | ### timeToMillis 206 | 207 | ``timeToMillis`` allow to convert ``time`` string to ``millseconds`` 208 | 209 | ```java 210 | int milliseconds = DateTimeUtils.timeToMillis("14:20"); // 860000 211 | ``` 212 | 213 | 214 | 215 | ## Author 216 | 217 | - **Thunder413** (https://github.com/thunder413) 218 | 219 | ## License 220 | 221 | This project is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php) 222 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | applicationId "com.github.thunder413.datetimeutilssample" 8 | minSdkVersion 14 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(include: ['*.jar'], dir: 'libs') 24 | implementation 'com.android.support:appcompat-v7:28.0.0' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | implementation project(':DateTimeUtils') 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Crowman\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/github/thunder413/datetimeutilssample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutilssample; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.github.thunder413.datetimeutilssample", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/thunder413/datetimeutilssample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.thunder413.datetimeutilssample; 2 | 3 | import android.os.Bundle; 4 | 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.Toolbar; 7 | import android.text.format.DateUtils; 8 | import android.util.Log; 9 | import android.view.View; 10 | import android.view.Menu; 11 | import android.view.MenuItem; 12 | 13 | import com.github.thunder413.datetimeutils.DateTimeStyle; 14 | import com.github.thunder413.datetimeutils.DateTimeUnits; 15 | import com.github.thunder413.datetimeutils.DateTimeUtils; 16 | 17 | import java.time.ZoneId; 18 | import java.util.Calendar; 19 | import java.util.Date; 20 | import java.util.Locale; 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | private static final String TAG = "MainActivity"; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | 31 | Date date = new Date(); 32 | DateTimeUtils.setDebug(true); 33 | 34 | Log.d(TAG,"Date To String >> "+DateTimeUtils.formatDate(new Date())); 35 | DateTimeUtils.setTimeZone("GMT"); 36 | Log.d(TAG,"Previous month from today >> "+DateTimeUtils.formatDate(DateTimeUtils.getPreviousMonthDate(new Date()))); 37 | Log.d(TAG,"Next month from today >> "+DateTimeUtils.formatDate(DateTimeUtils.getNextMonthDate(new Date()))); 38 | Log.d(TAG,"Previous >> "+DateTimeUtils.formatDate(DateTimeUtils.getPreviousWeekDate(DateTimeUtils.formatDate(" 2019-05-06 22:32:57"), Calendar.MONDAY))); 39 | /*Log.d(TAG,"String To Date >> "+DateTimeUtils.formatDate("2017-06-13 04:14:49")); 40 | Log.d(TAG,"IsToDay >> "+DateTimeUtils.isToday(new Date())); 41 | Log.d(TAG,"IsToDay String >> "+DateTimeUtils.isToday("2017-06-15 04:14:49")); 42 | Log.d(TAG,"IsYesterdaY Date >> "+DateTimeUtils.isYesterday(new Date())); 43 | Log.d(TAG,"IsYesterdaY String >> "+DateTimeUtils.isYesterday("2017-06-12 04:14:49")); 44 | Log.d(TAG,"TimeAgo String >> "+DateTimeUtils.getTimeAgo(this,"2017-06-13 04:14:49")); 45 | Log.d(TAG,"TimeAgo Date >> "+DateTimeUtils.getTimeAgo(this,date)); 46 | Log.d(TAG,"Diff in milliseconds >> "+DateTimeUtils.getDateDiff(new Date(),DateTimeUtils.formatDate("2017-06-13 04:14:49"), DateTimeUnits.MILLISECONDS)); 47 | Log.d(TAG,"Diff in seconds >> "+DateTimeUtils.getDateDiff(new Date(),DateTimeUtils.formatDate("2017-06-13 04:14:49"), DateTimeUnits.SECONDS)); 48 | Log.d(TAG,"Diff in minutes >> "+DateTimeUtils.getDateDiff(new Date(),DateTimeUtils.formatDate("2017-06-13 04:14:49"), DateTimeUnits.MINUTES)); 49 | Log.d(TAG,"Diff in hours >> "+DateTimeUtils.getDateDiff(new Date(),DateTimeUtils.formatDate("2017-06-13 04:14:49"), DateTimeUnits.HOURS)); 50 | Log.d(TAG,"Diff in days >> "+DateTimeUtils.getDateDiff(new Date(),DateTimeUtils.formatDate("2017-06-13 04:14:49"), DateTimeUnits.DAYS)); 51 | Log.d(TAG,"Extract time from date >> "+DateTimeUtils.formatTime(new Date())); 52 | Log.d(TAG,"Extract time from dateString >> "+DateTimeUtils.formatTime("2017-06-13 04:14:49")); 53 | Log.d(TAG,"Millis to time >> "+DateTimeUtils.millisToTime(25416660)); 54 | Log.d(TAG,"Time to millis >> "+DateTimeUtils.timeToMillis("14:20")); 55 | Log.d(TAG,"Revert Millis to time >> "+DateTimeUtils.millisToTime(860000)); 56 | Log.d(TAG,"FormatWithStyle FULL >> "+DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.FULL)); 57 | Log.d(TAG,"FormatWithStyle LONG >> "+DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.LONG)); 58 | Log.d(TAG,"FormatWithStyle MEDIUM >> "+DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.MEDIUM)); 59 | Log.d(TAG,"FormatWithStyle SHORT >> "+DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.SHORT));*/ 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thunder413/DateTimeUtils/e4837dd25e728187c5d2700d1cc959e843f41041/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 16dp 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DateTimeUtilsSample 3 | Settings 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |