DateTimeUtils


Source link: https://github.com/thunder413/DateTimeUtils

DateTimeUtils

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.

Usage

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:

compile 'com.github.thunder413:DateTimeUtils:1.0'

Javadocs are available here.

Examples

setTimeZone

setTimeZone allow you to define your time zone by default it's UTC

DateTimeUtils.setTimeZone("UTC");

formatDate

formatDate is a method that allow you to convert date object to string or timeStamp to date and vice-versa.

Date string to Date object

// 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");

Date object to date string MySQL/SQLite

String date = DateTimeUtils.formatDate(new Date());

timeStamp to Date object

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

formatWithStyle allow to parse date into localized format using most common style

Date object to localized date

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

Date string to localized date

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

formatWithPattern allow to define your own parse pattern following SimpleDateFormat scheme

Date string as source

DateTimeUtils.formatWithPattern("2017-06-13", "EEEE, MMMM dd, yyyy");
 // Tuesday, June 13, 2017

Date object as source

DateTimeUtils.formatWithPattern(new Date(), "EEEE, MMMM dd, yyyy");
 // Tuesday, June 13, 2017

isToday

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

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");

getDateDiff

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

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

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

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

timeToMillis allow to convert time string to millseconds

int milliseconds = DateTimeUtils.timeToMillis("14:20");
 // 860000

Author

License

This project is licensed under the MIT License

Resources

A general purpose android UI library to show a user show menu in accordance of Floating action button with modern way of material design guidelines.

A showcase of RxJava and Model View Presenter, plus a number of other popular libraries for android development, including AutoValue, Retrofit, Moshi, and ButterKnife. Unit tests covering any business logic and Robolectric tests verifying the ui.

Converts a string to a slug.

A JUnit rule which allows unit testing of custom Lint rules.

A notification badge with animation.

This plugin helps to develop with Android Data Binding Library.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes