Skip to content
This repository was archived by the owner on Jan 22, 2023. It is now read-only.

Commit 071174f

Browse files
author
Jonas Schubert
committed
adds new long extensions to format long in human readable string
1 parent d622c6a commit 071174f

File tree

3 files changed

+158
-11
lines changed

3 files changed

+158
-11
lines changed

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# TimeXt-Java
22

33
[![Platform](https://img.shields.io/badge/platform-Android-blue.svg)](https://www.android.com)
4-
[![API](https://img.shields.io/badge/API-14+-blue.svg)](https://android-arsenal.com/api?level=14)
5-
[![Version](https://img.shields.io/badge/version-v0.2.1-blue.svg)](https://github.com/TimeXt/TimeXt-Java/tree/v0.2.1)
4+
[![API](https://img.shields.io/badge/API-26+-blue.svg)](https://android-arsenal.com/api?level=26)
5+
6+
[![Version](https://img.shields.io/badge/version-0.3.0-blue.svg)](https://github.com/TimeXt/TimeXt-Java/tree/0.3.0)
67
[![Build](https://img.shields.io/badge/buildJava-success-green.svg)](releases/lib-2019-05-02-1.jar)
7-
[![Build](https://img.shields.io/badge/buildAndroid-success-green.svg)](releases/lib-android-2019-05-02-1.aar)
88

99
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
1010
[![Paypal](https://img.shields.io/badge/paypal-donate-blue.svg)](https://www.paypal.me/GuepardoApps)
@@ -15,9 +15,13 @@ First of all many thanks to [Kizitonwose](https://github.com/kizitonwose/Time) f
1515
This library shall help to reduce code like
1616

1717
```java
18-
int dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
18+
int dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
1919
```
2020

21+
## Important
22+
23+
Better use the [Kotlin version](https://github.com/TimeXt/TimeXt-Kotlin/) of this library!
24+
2125
## How to use
2226

2327
### Basics
@@ -35,7 +39,7 @@ Interval oneNanoSecond = NumberExtensions.toNanoSeconds(1);
3539
Interval onePicoSecond = NumberExtensions.toPicoSeconds(1);
3640

3741
double oneDayInMillis = NumberExtensions.toDays(1).InMilliSeconds(); // Converts one day into milliseconds
38-
double twoWeeksInHours = NumberExtensions.toWeeks(2).InHours(); // Converts two weeks into hours
42+
double twoWeeksInHours = NumberExtensions.toWeeks(2).InHours(); // Converts two weeks into hours
3943

4044
Interval duration = NumberExtensions.toDays(1).Plus(NumberExtensions.toHours(6));
4145
Interval difference = NumberExtensions.toMinutes(34).Minus(NumberExtensions.toSeconds(420));
@@ -75,6 +79,15 @@ Runnable runnable = ...;
7579
HandlerExtensions.PostDelayed(handler, runnable, new Interval(2, Unit.Minute));
7680
```
7781

82+
Since version 0.3.0 TimeXt has new extensions for the long type to display this number value in a human readable string format.
83+
84+
```java
85+
val readableStringFromMilliseconds = LongExtensions.formatMilliseconds(34325055574) // 56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds
86+
val readableStringFromSeconds = LongExtensions.formatSeconds(4350554) // 7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds
87+
val readableStringFromMinutes = LongExtensions.formatMinutes(432555) // 42 weeks, 6 days, 9 hours, 15 minutes
88+
val readableStringFromHours = LongExtensions.formatHours(4574) // 27 weeks, 1 day, 14 hours
89+
```
90+
7891
## Requirements
7992

8093
- Use at least JVM 1.8
@@ -83,6 +96,26 @@ HandlerExtensions.PostDelayed(handler, runnable, new Interval(2, Unit.Minute));
8396

8497
TimeXt-Java is distributed under the MIT license. [See LICENSE](LICENSE.md) for details.
8598

86-
## Additional
87-
88-
Better use the [Kotlin version](https://github.com/TimeXt/TimeXt-Kotlin/) of this library!
99+
```
100+
MIT License
101+
102+
Copyright (c) 2018 - 2019 GuepardoApps (Jonas Schubert)
103+
104+
Permission is hereby granted, free of charge, to any person obtaining a copy
105+
of this software and associated documentation files (the "Software"), to deal
106+
in the Software without restriction, including without limitation the rights
107+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
108+
copies of the Software, and to permit persons to whom the Software is
109+
furnished to do so, subject to the following conditions:
110+
111+
The above copyright notice and this permission notice shall be included in all
112+
copies or substantial portions of the Software.
113+
114+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
115+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
116+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
117+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
118+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
119+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
120+
SOFTWARE.
121+
```

lib-android/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ android {
66

77

88
defaultConfig {
9-
minSdkVersion 14
9+
minSdkVersion 26
1010
targetSdkVersion 28
11-
versionCode 201
12-
versionName "0.2.1"
11+
versionCode 300
12+
versionName "0.3.0"
1313
}
1414

1515
buildTypes {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package guepardoapps.timext.java.extensions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Locale;
5+
6+
public class LongExtensions {
7+
public static String formatMilliseconds(long milliseconds) {
8+
ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>();
9+
dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24 * 60 * 60 * 1000L, Integer.MAX_VALUE)));
10+
dictionary.add(new Tuple<>("day", new Tuple<>(24 * 60 * 60 * 1000L, 7)));
11+
dictionary.add(new Tuple<>("hour", new Tuple<>(60 * 60 * 1000L, 24)));
12+
dictionary.add(new Tuple<>("minute", new Tuple<>(60 * 1000L, 60)));
13+
dictionary.add(new Tuple<>("second", new Tuple<>(1000L, 60)));
14+
dictionary.add(new Tuple<>("millisecond", new Tuple<>(1L, 1000)));
15+
16+
ArrayList<String> arrayList = new ArrayList<>();
17+
dictionary.forEach(value -> {
18+
long testValue = ((milliseconds / value.second.first) % value.second.second);
19+
if (testValue > 1) {
20+
arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first));
21+
} else if (testValue > 0) {
22+
arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first));
23+
}
24+
});
25+
26+
if (arrayList.size() > 0) {
27+
return String.join(", ", arrayList);
28+
} else {
29+
return "0 milliseconds";
30+
}
31+
}
32+
33+
public static String formatSeconds(long seconds) {
34+
ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>();
35+
dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24 * 60 * 60L, Integer.MAX_VALUE)));
36+
dictionary.add(new Tuple<>("day", new Tuple<>(24 * 60 * 60L, 7)));
37+
dictionary.add(new Tuple<>("hour", new Tuple<>(60 * 60L, 24)));
38+
dictionary.add(new Tuple<>("minute", new Tuple<>(60L, 60)));
39+
dictionary.add(new Tuple<>("second", new Tuple<>(1L, 60)));
40+
41+
ArrayList<String> arrayList = new ArrayList<>();
42+
dictionary.forEach(value -> {
43+
long testValue = ((seconds / value.second.first) % value.second.second);
44+
if (testValue > 1) {
45+
arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first));
46+
} else if (testValue > 0) {
47+
arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first));
48+
}
49+
});
50+
51+
if (arrayList.size() > 0) {
52+
return String.join(", ", arrayList);
53+
} else {
54+
return LongExtensions.formatMilliseconds(1000* seconds);
55+
}
56+
}
57+
58+
public static String formatMinutes(long minutes) {
59+
ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>();
60+
dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24 * 60L, Integer.MAX_VALUE)));
61+
dictionary.add(new Tuple<>("day", new Tuple<>(24 * 60L, 7)));
62+
dictionary.add(new Tuple<>("hour", new Tuple<>(60L, 24)));
63+
dictionary.add(new Tuple<>("minute", new Tuple<>(1L, 60)));
64+
65+
ArrayList<String> arrayList = new ArrayList<>();
66+
dictionary.forEach(value -> {
67+
long testValue = ((minutes / value.second.first) % value.second.second);
68+
if (testValue > 1) {
69+
arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first));
70+
} else if (testValue > 0) {
71+
arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first));
72+
}
73+
});
74+
75+
if (arrayList.size() > 0) {
76+
return String.join(", ", arrayList);
77+
} else {
78+
return LongExtensions.formatSeconds(60 * minutes);
79+
}
80+
}
81+
82+
public static String formatHours(long hours) {
83+
ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>();
84+
dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24L, Integer.MAX_VALUE)));
85+
dictionary.add(new Tuple<>("day", new Tuple<>(24L, 7)));
86+
dictionary.add(new Tuple<>("hour", new Tuple<>(1L, 24)));
87+
88+
ArrayList<String> arrayList = new ArrayList<>();
89+
dictionary.forEach(value -> {
90+
long testValue = ((hours / value.second.first) % value.second.second);
91+
if (testValue > 1) {
92+
arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first));
93+
} else if (testValue > 0) {
94+
arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first));
95+
}
96+
});
97+
98+
if (arrayList.size() > 0) {
99+
return String.join(", ", arrayList);
100+
} else {
101+
return LongExtensions.formatMinutes(60 * hours);
102+
}
103+
}
104+
105+
private static class Tuple<T, K> {
106+
final T first;
107+
final K second;
108+
109+
Tuple(T first, K second) {
110+
this.first = first;
111+
this.second = second;
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)