GithubHelp home page GithubHelp logo

marcinorlowski / datetimetemplate Goto Github PK

View Code? Open in Web Editor NEW
45.0 6.0 5.0 249 KB

Android date/time formatting library supporting formatting placeholders

Java 65.11% Kotlin 34.89%
android android-library date-formatting datetime

datetimetemplate's Introduction

Release Downloads Dependency Status

DateTimeTemplate

DateTimeTemplate is flexible date/time formatting library with placeholders support.

Demo app

Download demo application APK from releases section. Source code in project's app/ module.

Features

  • Easy to use,
  • Stable and production ready,
  • Localization support,
  • Lightweight,
  • No extra dependencies,
  • Free.

Installation

Edit your master gradle.build file and add maven { url 'https://jitpack.io' } to your current repositories block content (if you use other jitpack hosted libraries, then this step can be skipped):

allprojects {
  repositories {
    maven { url 'https://jitpack.io' }
    }
}

Next, edit your module's build.gradle and the following dependency:

compile 'com.github.MarcinOrlowski:datetimetemplate:<VERSION>'

For recent value of <VERSION> consult library releases or jitpack badge: Release

Placeholders

Your formatting string can contain anything you like, however certain sequences are considered placeholders, and will be replaced by corresponding values. Non-placeholders are returned unprocessed

Placeholder Description
%yy% long year (i.e. "2009")
%y% short year (i.e. "09")
%MMM% long month name (i.e. "January")
%MM% abbreviated month name (i.e. "Jan")
%M% first letter of month name (i.e. "J")
%mm% zero prefixed 2 digit month number ("02" for Feb, "12" for Dec)
%m% month number as is ("2" for Feb, "12" for Dec)
%DDD% full day name (i.e. ""Saturday", "Sunday", "Monday")
%DD% abbreviated day name ("Sat", "Sun", "Mon")
%D% one letter day name ("S", "S", "M")
%dd% zero prefixed 2 digit day number ("01", "27")
%d% day number as is ("1", "27")
%dy% day number of the year (i.e. "250")
%dw% day number in week (i.e. "1" for Monday if weeks start on Mondays!)
%wm% week number of current month ("3" for 3rd week)
%wy% week number of the year ("3" for 3rd week, 47 for 47th)
%hh% current hour, zero prefixed, 24hrs clock (i.e. "01", "16")
%h% current hour, 24hrs clock (i.e. "1", "16")
%kk% current hour, zero prefixed, 12hrs clock (i.e. "01", "11")
%k% current hour, 12hrs clock (i.e. "1", "11")
%ii% current minute, zero prefixed (i.e. "01", "35")
%i% current minute, zero prefixed (i.e. "1", "35")
%AA% upper-cased AM/PM marker (i.e. "AM")
%A% upper-cased abbreviated AM/PM marker. "A" for "AM", "P" for "PM"
%aa% lower-cased am/pm marker (i.e. "am")
%a% lower-cased abbreviated AM/PM marker. "a" for "am", "p" for "pm"
%Aa% AM/PM marker with first letter uppercased (i.e. "Am"/"Pm")

Examples

Format current date, time as it is in GMT zone, using 24hrs clock format:

TimeZone tz = TimeZone.getTimeZone("GMT");
Calendar c = new GregorianCalendar(tz);
c.setTime(new Date());

String formatted = DateTimeTemplate.format(c, "GMT: %DD% %hh%:%ii%");

would produce GMT: Mon 16:25.

Format current time as it is in Pacific Daylight Time zone (PDT is 7 hours behind GMT), using 12hrs clock format with abbreviated AM/PM marker:

TimeZone tz = TimeZone.getTimeZone("GMT-0700");
Calendar c = new GregorianCalendar(tz);
c.setTime(new Date());

String formatted = DateTimeTemplate.format(c, "Time: %k%:%ii%%a%");

would produce Time: 3:25a.

Format current time using device's time zone:

TimeZone tz = TimeZone.getDefault();
Calendar c = new GregorianCalendar(tz);
c.setTime(new Date());

String formatted = DateTimeTemplate.format(c, ...);

Formatting using locale of your choice:

String formatted = DateTimeTemplate.format(cal, "Time: %k%:%ii%%a%", Locale.SIMPLIFIED_CHINESE);

Contributing

Please report any issue spotted using GitHub's project tracker.

If you'd like to contribute to the this project, please open new ticket before doing any work. This will help us save your time in case I'd not be able to accept such changes. But if all is good and clear then follow common routine:

  • fork the project
  • create new branch
  • do your changes
  • send pull request

License

datetimetemplate's People

Contributors

borszczuk avatar marcinorlowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

datetimetemplate's Issues

%wy% for Jan 1st 2012

It looks that "wy" for Jan 1st, 2012 returns 53 instead of 1 Java docs does not say it's correct. Need to check if that is still Android OS version related and can be no longer bothered or we need a workaround here.

Question: How to display 12:30 AM or 12:30 PM in a 12 hour format?

Hello Dear,

I was trying to display 12:30 AM or 12:30 PM in a 12 hour format but your library is displaying only 00:30 AM or 00:30 PM. Is it possible to display my type of format into your library or Am I doing something wrong?

My Current format is "%d% %MM% %y%, %kk%:%ii% %AA%"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.