Android library that regroup bunch of dateTime utilities

Overview

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:

implementation 'com.github.thunder413:DateTimeUtils:3.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");

Get Previous next Week

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

Get Previous next month

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

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

Comments
  • How to get the previous week or month date using the given date ?

    How to get the previous week or month date using the given date ?

    Hi

    Thanks you very much for this library,

    Is this library supports to get the get the previous week or month date using the given date?

    Eg: 1. Given date 2019-04-03 previous week date 2019-04-26 2. Given date 2019-04-29 previous 12 month date 2019-04-30

    opened by ArunVicky001 3
  • Getting Error while compiling library

    Getting Error while compiling library

    Error:A problem occurred configuring project ':app'.

    Could not resolve all dependencies for configuration ':app:_debugApkCopy'. Could not find com.github.thunder413:DateTimeUtils:1.3. Required by: project :app

    opened by chnouman 3
  • Changing timezone india

    Changing timezone india

    Hey, Thanks for this beautiful library. I am not able to change the timezone to IST(Indian standard time) as the library has a default timezone UTC. I have added a line of code that you mentioned in the about section in my launcher activity. DateTimeUtils.setTimeZone("IST");

    opened by cybertronjc 2
  • The

    The "formatDate()" method does not return the time correctly.

    Hello, when I use the "formatDate()" method, the time that I send does not correspond to the time that it returns. The date is returned correctly. What would be the solution? Thanks.

    opened by bytevel 0
  • strings.xml error

    strings.xml error

    After using library, my app name changed to DateTimeUtil. I think there is a conflict in values xml files. I see en- strings xml and there is a string value with a key name app_name too.

    opened by kaungkhantsoe 0
  • DateTimeUtils Andoird change Name of Application

    DateTimeUtils Andoird change Name of Application

    I don't know if is an error, but when i use this library and i compile my projet, n-the name of my app change.

    implementation 'com.github.thunder413:DateTimeUtils:3.0'

    opened by Danielbaleba 1
  • Spanish support for getTimeAgo.

    Spanish support for getTimeAgo.

    Hello, first of all, thank you for this great library! =D.

    Im just asking if you can add suport for spanish in getTimeAgo, or where i can change that in the library.

    opened by Diegofr104 0
Releases(3.0)
Owner
Thunder413
Senior Web & Mobile Application Developper
Thunder413
CreditCardHelper 🖊️ A Jetpack-Compose library providing useful credit card utilities such as card type recognition and TextField ViewTransformations

CreditCardHelper ??️ A Jetpack-Compose library providing useful credit card utilities such as card type recognition and TextField ViewTransformations

Stelios Papamichail 18 Dec 19, 2022
Collection of source codes, utilities, templates and snippets for Android development.

Android Templates and Utilities [DEPRECATED] Android Templates and Utilities are deprecated. I started with this project in 2012. Android ecosystem ha

Petr Nohejl 1.1k Nov 30, 2022
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

Markus Junginger 1.4k Dec 29, 2022
Utilities I wish Android had but doesn't

wishlist Library of helpers and utilities that I wish were included in the Android SDK but aren't. If you think something in this library is already h

Kevin Sawicki 386 Nov 21, 2022
Various useful utilities for Android apps development

Android Commons Various useful utilities for Android apps development. API documentation provided as Javadoc. Usage Add dependency to your build.gradl

Alex Vasilkov 112 Nov 14, 2022
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

Markus Junginger 1.4k Dec 29, 2022
A Telegram bot utilities that help to reduce the code amount

Flume Party A Telegram bot utilities that help to reduce code amount. Real project examples Pull Party Bot: 19% of code has been reduced. Resistance B

pool party 1 Jun 8, 2022
FractalUtils - A collection of utility functions and classes, with an emphasis on game related utilities

A collection of utility functions and classes written in Kotlin. There is some emphasis on utilities useful for games (Geometry, Random, Time, Updating, etc).

null 2 Nov 11, 2022
Little utilities for more pleasant immutable data in Kotlin

What can KopyKat do? Mutable copy Nested mutation Nested collections Mapping copyMap copy for sealed hierarchies copy from supertypes copy for type al

KopyKat 193 Dec 19, 2022
Android library which makes it easy to handle the different obstacles while calling an API (Web Service) in Android App.

API Calling Flow API Calling Flow is a Android library which can help you to simplify handling different conditions while calling an API (Web Service)

Rohit Surwase 19 Nov 9, 2021
A robust native library loader for Android.

ReLinker A robust native library loader for Android. More information can be found in our blog post Min SDK: 9 JavaDoc Overview The Android PackageMan

Keepsafe 2.9k Dec 27, 2022
Joda-Time library with Android specialization

joda-time-android This library is a version of Joda-Time built with Android in mind. Why Joda-Time? Android has built-in date and time handling - why

Daniel Lew 2.6k Dec 9, 2022
UPnP/DLNA library for Java and Android

Cling EOL: This project is no longer actively maintained, code may be outdated. If you are interested in maintaining and developing this project, comm

4th Line 1.6k Jan 4, 2023
:iphone: [Android Library] Get device information in a super easy way.

EasyDeviceInfo Android library to get device information in a super easy way. The library is built for simplicity and approachability. It not only eli

Nishant Srivastava 1.7k Dec 22, 2022
Android library for viewing, editing and sharing in app databases.

DbInspector DbInspector provides a simple way to view the contents of the in-app database for debugging purposes. There is no need to pull the databas

Infinum 924 Jan 4, 2023
Android Market In-app Billing Library

Update In-app Billing v2 API is deprecated and will be shut down in January 2015. This library was developed for v2 a long time ago. If your app is st

Robot Media 533 Nov 25, 2022
An Android library allowing images to exhibit a parallax effect that reacts to the device's tilt

Motion An Android library allowing images to exhibit a parallax effect. By replacing static pictures and backgrounds with a fluid images that reacts t

Nathan VanBenschoten 781 Nov 11, 2022
Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Anup Cowkur 667 Dec 22, 2022