android 格式化日期格式,DateTimeTemplate:Android日期/时间格式库支持格式化占位符...

625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png

DateTimeTemplate

DateTimeTemplate is date/time formatting library supporting formatting placeholders.

625332134c6f4d4600884b99daebf603.png

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:'

For recent value of consult library releases or jitpack badge: 625332134c6f4d4600884b99daebf603.png

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

Written and copyrighted ©2013-2018 by Marcin Orlowski

DateTimeTemplate is open-sourced software licensed under the MIT license

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
String.format 格式化日期是安全的。它能够根据指定的格式日期转换为所需的字符串表示形式。通过使用占位符格式字符串,可以根据需要自定义日期格式。在引用中的示例中,使用格式字符串"{0:D}"将当前日期格式化为"2009年3月20日"的形式。同样,引用中给出了使用*.ToString()方法来实现相同效果的示例,其中展示了多种日期格式的示例。 String.format 格式化日期的安全性在于它提供了灵活性和可靠性,可以根据需求对日期进行格式化,而不会引发任何安全问题。只要使用合适的格式字符串,并注意输入的日期的正确性,就可以安全地使用String.format 格式化日期。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [string.Format格式化用法详解](https://blog.csdn.net/a70642869/article/details/103762663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C#【基础篇】string.Format格式化日期](https://blog.csdn.net/sinat_40003796/article/details/104146583)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值