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.
This library is available under the MIT License.
The DateTimeUtils library is available from JitPack.
First add JitPack dependency line in your project build.gradle
file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And then simply add the following line to the dependencies
section of your app module build.gradle
file:
implementation 'com.github.thunder413:DateTimeUtils:3.0'
Javadocs are available here.
setTimeZone
allow you to define your time zone by default it's UTC
DateTimeUtils.setTimeZone("UTC");
formatDate
is a method that allow you to convert date object
to string
or timeStamp
to date and vice-versa.
// MySQL/SQLite dateTime example
Date date = DateTimeUtils.formatDate("2017-06-13 04:14:49");
// Or also with / separator
Date date = DateTimeUtils.formatDate("2017/06/13 04:14:49");
// MySQL/SQLite date example
Date date = DateTimeUtils.formatDate("2017-06-13");
// Or also with / separator
Date date = DateTimeUtils.formatDate("2017/06/13");
String date = DateTimeUtils.formatDate(new Date());
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
// Using milliseconds
Date date = DateTimeUtils.formatDate(1497399731000);
// Using seconds (Server timeStamp)
Date date = DateTimeUtils.formatDate(1497399731,DateTimeUnits.SECONDS);
formatWithStyle
allow to parse date into localized format using most common style
DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.FULL); // Tuesday, June 13, 2017
DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.LONG); // June 13, 2017
DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.MEDIUM); // Jun 13, 2017
DateTimeUtils.formatWithStyle(new Date(), DateTimeStyle.SHORT); // 06/13/17
DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.FULL); // Tuesday, June 13, 2017
DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.LONG); // June 13, 2017
DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.MEDIUM); // Jun 13, 2017
DateTimeUtils.formatWithStyle("2017-06-13", DateTimeStyle.SHORT); // 06/13/17
formatWithPattern
allow to define your own parse pattern following SimpleDateFormat
scheme
DateTimeUtils.formatWithPattern("2017-06-13", "EEEE, MMMM dd, yyyy"); // Tuesday, June 13, 2017
DateTimeUtils.formatWithPattern(new Date(), "EEEE, MMMM dd, yyyy"); // Tuesday, June 13, 2017
isToday
Tell whether or not a given date is today date
// Date object as source
boolean state = DateTimeUtils.isToday(new Date());
// Date String as source
boolean state = DateTimeUtils.isToday("2017-06-15 04:14:49");
isYesterday
Tell whether or not a given date is yesterday date
// Date object as source
boolean state = DateTimeUtils.isYesterday(new Date());
// Date String as source
boolean state = DateTimeUtils.isYestrday("2017-06-15 04:14:49");
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
// Date object as source
Date date = DateTimeUtils.getPreviousWeekDate(new Date(), Calendar.MONDAY);
// Date String as source
Date date = DateTimeUtils.getNextWeekDate("2017-06-15 04:14:49",Calendar.SUNDAY);
getPreviousMonthDate/getNextMonthDate
Return the next or a previous month date from a given date
// Date object as source
Date date = DateTimeUtils.getNextMonthDate(new Date());
// Date String as source
Date date = DateTimeUtils.getPreviousMonthDate("2017-06-15 04:14:49");
getDateDiff
give you the difference between two date in days, hours, minutes, seconds or milliseconds DateTimeUnits
// Dates can be date object or date string
Date date = new Date();
String date2 = "2017-06-13 04:14:49";
// Get difference in milliseconds
int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.MILLISECONDS);
// Get difference in seconds
int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.SECONDS);
// Get difference in minutes
int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.MINUTES);
// Get difference in hours
int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.HOURS);
// Get difference in days
int diff = DateTimeUtils.getDateDiff(date,date2, DateTimeUnits.DAYS);
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 :)
String timeAgo = DateTimeUtils.getTimeAgo(context,new Date()); // Full string style will be used
// Short string style
String timeAgo = DateTimeUtils.getTimeAgo(context,"new Date()",DateTimeStyle.AGO_SHORT_STRING );
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
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
// Solution >> force hours display
String time = DateTimeUtils.formatTime(new Date(),true);
// And you can also supplie a date string
String time = DateTimeUtils.formatTime("2017-06-13 04:14:49"); // 04:14:49
millisToTime
is usefull when your dealing with duration and want to display for example player duration or current playback position into human readable value.
String time = DateTimeUtils.millisToTime(2515); // It take millis as an argument not seconds
timeToMillis
allow to convert time
string to millseconds
int milliseconds = DateTimeUtils.timeToMillis("14:20"); // 860000
- Thunder413 (https://github.com/thunder413)
This project is licensed under the MIT License