I'm making a exchange rate app and I have a screen with a graph that shows changes of the selected currency in the last 7 days.
Now what I wanna get is to emit items in strict order.
Here is my code:
public class GraphInteractorImpl implements GraphInteractor {
private final Retrofit retrofit;
#Inject
public GraphInteractorImpl(Retrofit retrofit) {
this.retrofit = retrofit;
}
#Override
public void downloadData(GraphListener listener) {
RestAPI api = retrofit.create(RestAPI.class);
List<String> listDates = getDates();
for (String date : listDates) {
Observable<List<ExchangeRate>> observable = api.getExchangeRatesForLast7days(date);
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
listener::onSuccess,
listener::onFailure
);
}
}
private List<String> getDates() { //returns last 7 days in a list
List<String> listDate = new ArrayList<>();
Calendar calendarToday = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String today = simpleDateFormat.format(calendarToday.getTime());
Calendar calendarDayBefore = Calendar.getInstance();
calendarDayBefore.setTime(calendarDayBefore.getTime());
int daysCounter = 0;
while (daysCounter <= 7) {
if (daysCounter == 0) { // means that its present day
listDate.add(today);
} else { // subtracts 1 day after each pass
calendarDayBefore.add(Calendar.DAY_OF_MONTH, -1);
Date dateMinusOneDay = calendarDayBefore.getTime();
String oneDayAgo = simpleDateFormat.format(dateMinusOneDay);
listDate.add(oneDayAgo);
}
daysCounter++;
}
return listDate;
}
}
This code gets me the right values but they are not in order so I'm getting wrong values for specific days.
So what I have to do is execute 7 calls simultaneously, I'm guessing with zip operator but I didnt come up with a solution for this yet so any type of help would be appreciated.
API docs can be found here: http://hnbex.eu/api/v1/
So what I did to solve this is I added all the 7 observables in a list and then I just called the zipIterable() on that list
Related
I want to get all documents from a sub-collection.
Here's the path:
Collection(Shops)-Doc(shop)-Collection(year)-Doc(month)-Col(day)-Doc(Current)-Col(initial stocks).
This is how it looks like in firestore:
Numbers - Dhool - 2021 - 10(month) - 06(day) - Begining - initial stock
But I'm getting the following error:
I know there are other questions like that but they are not solving my problems.
Here is the code of the fragment where the query is :
public class DebutFragment extends Fragment {
Calendar calendar;
DatePickerDialog.OnDateSetListener date;
TextView dateView;
String day="", month="", year="";
public DebutFragment() {}
public static DebutFragment newInstance() {
return new DebutFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_actuel, container, false);
dateView = view.findViewById(R.id.date);
this.configureDate();
day = getDay(choosenDate);
month = getMonth(choosenDate);
year = getYear(choosenDate);
this.configureRecyclerView(view);
return view;
}
/////////////
//////////
private void configureDate() {
calendar = Calendar.getInstance();
date = (view, year, month, dayOfMonth) -> {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
};
updateLabel();
dateView.setOnClickListener(v -> new DatePickerDialog(getContext(), date,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show());
}
private void updateLabel() {
SimpleDateFormat dateFormatAbreger = new SimpleDateFormat("E dd MMM yy", Locale.FRANCE);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(BIRTHDAY_FORMAT, Locale.FRANCE);
dateView.setText(dateFormatAbreger.format(calendar.getTime()));
choosenDate = simpleDateFormat.format(calendar.getTime());
}
//////////////////
private void configureRecyclerView(View view) {
RecyclerView recyclerView = view.findViewById(R.id.khat_debut_recyclerview);
KhatDebutAdapter adapter = new KhatDebutAdapter(options);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter.notifyDataSetChanged();
}
Query query = getKhatInitialeCollectionReference(day, month, year).orderBy("typ", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Khat> options = new FirestoreRecyclerOptions.Builder<Khat>()
.setQuery(query, Khat.class).setLifecycleOwner(this).build();
}
Here is the code of all my Firebase methods :
public class FirebaseCalls {
//BOSSES
public static CollectionReference getBossesCollectionReference() {
return FirebaseFirestore.getInstance().collection(PATRONS);
}
public static DocumentReference getBossDocumentReference(String emailBoss) {
return getBossesCollectionReference().document(emailBoss);
}
//NUMBERS
public static CollectionReference getNumbersBigCollectionReference() {
return FirebaseFirestore.getInstance().collection(NUMBERS);
}
public static CollectionReference getNumbersCollectionReference() {
return getBossDocumentReference(currentBoss.getEmail()).collection(NUMBERS);
}
public static DocumentReference getNumberDocumentReference(String name) {
return getNumbersCollectionReference().document(name);
}
public static DocumentReference getNumberBigDocumentReference() {
return getNumbersBigCollectionReference().document(currentNumber.getName());
}
///YEAR
public static CollectionReference getYearCollectionReference(String year) {
return getNumberBigDocumentReference().collection(year);
}
///MONTH
public static DocumentReference getMonthDocumentReference(String month, String year) {
return getYearCollectionReference(year).document(month);
}
public static CollectionReference getDayCollectionReference(String day, String month, String year) {
return getMonthDocumentReference(month, year).collection(day);
}
///COMMENCEMENT
public static DocumentReference getCommencementDocumentReference(String day, String month, String year) {
return getDayCollectionReference(day, month, year).document(COMMENCEMENT);
}
///RESTANT
public static DocumentReference getRestantDocumentReference(String day, String month, String year) {
return getDayCollectionReference(day, month, year).document(RESTANT);
}
Can somebody help me, please?
This issue, as you mentioned, is already pointed out in different questions, and it's related to the Firestore data model. Here you can find detailed information about how this data model is structured. Basically, the error says you can’t have an odd number of segments to refer to a collection; this is because collections are always going to be in an odd position due to how hierarchy structures are designed. The documentation says:
Notice the alternating pattern of collections and documents. Your
collections and documents must always follow this pattern. You cannot
reference a collection in a collection or a document in a document.
You can see the pattern here:
And here is an example used in the documentation implemented in a Chat Application to explain Hierarchy structure:
To understand how hierarchical data structures work in Cloud
Firestore, consider an example chat app with messages and chat rooms.
You can create a collection called “rooms” to store different chat
rooms:
And this is how it’s referenced:
DocumentReference messageRef = db
.collection("rooms").document("roomA")
.collection("messages").document("message1");
This, compared to your application, will be:
Numbers - Dhool - 2021(year) - 10(month) - 06(day) - Beginning -
initial stock
Collection: Numbers
Document: Dhool
Subcollection: 2021(year)
Document: 10(month)
Subcollection: 06(day)
Document: Beginning
Subcollection: Initial Stock
And should be referenced in this way:
CollectionReference NumbersColRef = db.Collection(“Numbers”);
DocumentReference DhoolDocRef = NumbersColRef().Document(“Dhool”);
CollectionReference YearColRef = DhoolDocRef().Collection(“Year”);
DocumentReference MonthDocRef = YearColRef().Document(“Month”);
CollectionReference DayColRef = MonthDocRef().Collection(“Day”);
DocumentReference BeginningDocRef = DayColRef().Document(“Beginning”);
CollectionReference IStockColRef = BeginningDocRef().Collection(“IStock”);
If your structure is already like this, which I can see it is, the error may also occur when the references are not properly called or if one of them is empty or returning an empty or incorrect format value, which might be the case. You can troubleshoot your parameters and see if they are correctly called and also check the entity IDs if they return a type of value format that might cause a problem. The dates may contain a “/” or another symbol that could cause the program to crash, so be sure to parse it to Strings or Integers, or that the values are not being received in a format that could cause this issue. This was found in another forum with the same error, but the cause was a little different from what the error says. Please take in consideration this is not from the official documentation.
I am new for using RxJava and Room. What I trying to do is run a for loop to get data from database. The for loop iterate from first day of month to the last day of month.
Here is the Dao for this query.
#Query("SELECT SUM(duration) FROM xxx WHERE timeStamp >= :start and timeStamp <= :end and userId = :userId")
Flowable<Integer> getDuration(String userId, long start, long end);
And Here is how i using RxJava to get the result.
Calendar day1 = Calendar.getInstance();
Calendar day2 = Calendar.getInstance();
int maxLoopIndex = day1.getActualMaximum(Calendar.DAY_OF_MONTH);
day1.setFirstDayOfWeek(Calendar.MONDAY);
day2.setFirstDayOfWeek(Calendar.MONDAY);
day1.set(Calendar.DATE, day1.getActualMinimum(Calendar.DATE));
day2.set(Calendar.DATE, day2.getActualMinimum(Calendar.DATE));
day1.set(Calendar.HOUR_OF_DAY, 0);
day1.set(Calendar.MINUTE, 0);
day1.set(Calendar.SECOND, 0);
day1.set(Calendar.MILLISECOND, 0);
day2.set(Calendar.HOUR_OF_DAY, 23);
day2.set(Calendar.MINUTE, 59);
day2.set(Calendar.SECOND, 59);
day2.set(Calendar.MILLISECOND, 999);
ArrayList<Pair<Long, Long>> maxDayCount = new ArrayList<>();
//Get all the timeStamp in a month, where maxDayCount can be 30, 31, 28, 29.
for (int i = 0; i < maxDayCount; i++) {
Pair<Long, Long> P = Pair.create(day1.getTimeInMillis(), day2.getTimeInMillis());
pairArrayList.add(P);
day1.add(Calendar.DATE, 1);
day2.add(Calendar.DATE, 1);
}
// Using Flowable.formIterable to run through the list and get the data from room
Flowable.fromIterable(pairArrayList)
.flatMap(new Function<Pair<Long, Long>, Flowable<Integer>>() {
#Override
public Flowable<Integer> apply(#NonNull Pair<Long, Long> date) throws Exception {
return roomdb.Dao().getDuration(
User.getCurUser().getId(), date.first, date.second
);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Integer>() {
#Override
public void accept(#NonNull Integer source) throws Exception {
Log.d(TAG, "Duration: "+source);
// I want to get the index of pairArrayList to store the duration in
// corresponding array
}
});
However in subscribe I can get the result return by room however I can not get which index is run in pairArrayList. Is there any way I can get the index? Furthermore is there any better way to get data from room with the loop?
Let's begin with the final structure. It should contain the day of month and duration:
class DayDuration {
public Integer day;
public Long duration;
public DayDuration(Integer day, Long duration) {
this.day = day;
this.duration = duration;
}
#Override
public boolean equals(Object o) { /* implementation */ }
#Override
public int hashCode() { /* implementation */ }
}
Creation of final Flowable what emits requested items might look like the following code. I have used ThreetenBP library to handle date/time operations because Android Calendar API is pure hell. Recommend you do the same:
class SO64870062 {
private Flowable<Long> getDuration(String userId, long start, long end) {
return Flowable.fromCallable(() -> start); // mock data
}
#NotNull
private Flowable<LocalDate> getDaysInMonth(YearMonth yearMonth) { // (1)
LocalDate start = LocalDate.of(yearMonth.getYear(), yearMonth.getMonthValue(), 1);
LocalDate end = start.with(TemporalAdjusters.lastDayOfMonth());
return Flowable.create(emitter -> {
LocalDate current = start;
while (!current.isAfter(end)) { // (2)
emitter.onNext(current);
current = current.plusDays(1);
}
emitter.onComplete();
}, BackpressureStrategy.BUFFER);
}
#NotNull
private Flowable<DayDuration> getDurationForDay(String userId, LocalDate localDate) {
long startDayMillis = localDate.atStartOfDay().atZone(ZoneOffset.UTC) // (3)
.toInstant()
.toEpochMilli();
long endDayMillis = localDate.atTime(LocalTime.MAX).atZone(ZoneOffset.UTC)
.toInstant()
.toEpochMilli();
return getDuration(userId, startDayMillis, endDayMillis) // (4)
.map(duration -> new DayDuration(localDate.getDayOfMonth(), duration));
}
public Flowable<DayDuration> getDayDurations(String userId, YearMonth yearMonth) {
return getDaysInMonth(yearMonth)
.flatMap(localDate -> getDurationForDay(userId, localDate));
}
}
Important and interesting parts:
Function getDaysInMonth() creates Flowable what emits all days of requested month.
Iteration from start (first day of a month) to end (last day of a month) date and emitting all of the days.
Make sure you set the zone you use within timestamps in your database. I have used UTC for simplicity.
Combine duration from a database with the current date.
Last but not least, let's check if it works correctly:
public class SO64870062Test {
#Test
public void whenDaysRequestedForApril2020ThenEmitted() {
SO64870062 tested = new SO64870062();
TestSubscriber<DayDuration> testSubscriber = tested
.getDayDurations("userId", YearMonth.of(2020, 11))
.test();
testSubscriber.assertValueCount(30);
testSubscriber.assertValueAt(1, new DayDuration(2, 1604275200000L));
testSubscriber.assertComplete();
}
}
How can I customize the X axis to show days (or months or years, based on the selected range) where a new day/month/year begins? I am using CategoryDateAxis (CreateMultiPaneStockChartsFragment example).
What I want:
Larger ranges:
Smaller ranges (easily see where new day begins):
What I have:
Right now I am using default label provider and it is hard to see when new day/month/year begins. E.g. for 7 day range:
Axis is construced like this:
final CategoryDateAxis xAxis = sciChartBuilder.newCategoryDateAxis()
.withVisibility(isMainPane ? View.VISIBLE : View.GONE)
.withVisibleRange(sharedXRange)
//.withLabelProvider(new TradeChartAxisLabelProviderDateTime())
.withGrowBy(0.01d, 0.01d)
.build();
How do I achieve this?
public static class TradeChartAxisLabelProviderDateTime extends TradeChartAxisLabelProvider {
public TradeChartAxisLabelProviderDateTime() {
super();
}
#Override
public String formatLabel(Comparable dataValue) {
if(currentRange == RANGE_1_YEAR) {
} else if(currentRange == RANGE_1_MONTH) {
} else if(currentRange == RANGE_1_DAY) {
}
String text = super.formatLabel(dataValue).toString();
return text;
}
}
To implement selection of label based on VisibleRange you can use code like this:
public static class TradeChartAxisLabelProviderDateTime extends TradeChartAxisLabelProvider {
public TradeChartAxisLabelProviderDateTime() {
super(new TradeChartAxisLabelFormatterDateTime());
}
private static class TradeChartAxisLabelFormatterDateTime implements ILabelFormatter<CategoryDateAxis> {
private final SimpleDateFormat labelFormat, cursorLabelFormat;
private TradeChartAxisLabelFormatterDateTime() {
labelFormat = new SimpleDateFormat(CategoryDateAxis.DEFAULT_TEXT_FORMATTING, Locale.getDefault());
cursorLabelFormat = new SimpleDateFormat(CategoryDateAxis.DEFAULT_TEXT_FORMATTING, Locale.getDefault());
}
#Override
public void update(CategoryDateAxis axis) {
final ICategoryLabelProvider labelProvider = Guard.instanceOfAndNotNull(axis.getLabelProvider(), ICategoryLabelProvider.class);
// this is range of indices which are drawn by CategoryDateAxis
final IRange<Double> visibleRange = axis.getVisibleRange();
// convert indicies to range of dates
final DateRange dateRange = new DateRange(
ComparableUtil.toDate(labelProvider.transformIndexToData((int) NumberUtil.constrain(Math.floor(visibleRange.getMin()), 0, Integer.MAX_VALUE))),
ComparableUtil.toDate(labelProvider.transformIndexToData((int) NumberUtil.constrain(Math.ceil(visibleRange.getMax()), 0, Integer.MAX_VALUE))));
if (dateRange.getIsDefined()) {
long ticksInViewport = dateRange.getDiff().getTime();
// select formatting based on diff in time between Min and Max
if (ticksInViewport > DateIntervalUtil.fromYears(1)) {
// apply year formatting
labelFormat.applyPattern("");
cursorLabelFormat.applyPattern("");
} else if (ticksInViewport > DateIntervalUtil.fromMonths(1)) {
// apply month formatting
labelFormat.applyPattern("");
cursorLabelFormat.applyPattern("");
} else if (ticksInViewport > DateIntervalUtil.fromMonths(1)) {
// apply day formatting
labelFormat.applyPattern("");
cursorLabelFormat.applyPattern("");
}
}
}
#Override
public CharSequence formatLabel(Comparable dataValue) {
final Date valueToFormat = ComparableUtil.toDate(dataValue);
return labelFormat.format(valueToFormat);
}
#Override
public CharSequence formatCursorLabel(Comparable dataValue) {
final Date valueToFormat = ComparableUtil.toDate(dataValue);
return cursorLabelFormat.format(valueToFormat);
}
}
}
In update() you can get access to VisibleRange of axis and based on it select label formatting and then use SimpleDateFormat to format Dates.
But as I understand your case is more complex than this because you can't get labels which allow to see when new day/month/year begins based on current VisibleRange. For this case you'll need to select format string based on previously formatted values and track when day/month/year changes.
How to sort my list through Rx function, My list contain three type of different source then I want to display my list sorted using date, how to apply that using RxAndroid?
subscriptions.add(complaintsAPI.getComplaintsAPI(userDetails.getUsername())
.compose(ReactiveUtils.applySchedulers())
.map(list -> {
List<ComplaintsRowModel> rowModel = new ArrayList<>();
for (Complaint complaint : list.getComplaints()) {
rowModel.add(new ComplaintsRowModel(complaint.getDisputeNo(),
complaint.getOpenDate(), complaint.getArea(), complaint.getStatus()));
model.complaintsList.put(complaint.getDisputeNo(), complaint);
}
for (OnlineRequest onlineRequest : list.getOnlineRequests()) {
rowModel.add(new ComplaintsRowModel(onlineRequest.getRequestNo(), onlineRequest.getOpenDate(),
onlineRequest.getArea(), onlineRequest.getStatus()));
model.complaintsList.put(onlineRequest.getRequestNo(), onlineRequest);
}
for (LlTickets llTickets : list.getLlTickets()) {
rowModel.add(new ComplaintsRowModel(llTickets.getTicketNo(), llTickets.getOpenDate(),
llTickets.getType(), llTickets.getStatus()));
model.complaintsList.put(llTickets.getTicketNo(), llTickets);
}
return rowModel;}
).toSortedList(){
//how to sort here
}).subscribe(new RequestSubscriber<List<ComplaintsRowModel>>(view.getContext(), view.progressView) {
#Override
public void onFailure(RequestException requestException) {
view.showError(requestException);
}
#Override
public void onNoData() {
super.onNoData();
isAllDataLoaded = true;
view.noDataFound();
model.setNoDataFound(true);
}
#Override
public void onNext(List<ComplaintsRowModel> complaintsRowModels) {
isAllDataLoaded = true;
model.setRowModel(complaintsRowModels);
view.buildList(complaintsRowModels);
}
}));
I think in toSortedList() can I sort my list but I don't know the way to apply that.
The toSortedList operator would only work on Observable<ComplaintRowModel> while what you have is Observable<List<ComplaintRowModel>>. So first you have to transform your observable with
flatMapIterable(complaintRowModels -> complaintRowModels)
to map it to an observable of the list elements. Then you can apply the sorting something like
toSortedList((complaintRowModel, complaintRowModel2) -> {
Date date = complaintRowModel.getDate();
Date date2 = complaintRowModel2.getDate();
// comparing dates is very much dependent on your implementation
if (date <before> date2) {
return -1;
} else if (date <equal> date2) {
return 0;
} else {
return 1;
}
})
Then you get an observable of sorted list.
As per you don't want to provide specific information about your problem, there is generic answer.
When data object which need to be sorted implements Comparable or it's primitive type.
Observable.just(3, 2, 1)
.toSortedList()
.subscribe(list -> System.out.print(Arrays.toString(list.toArray())));
[1, 2, 3]
When data object which need to be sorted doesn't implement Comparable or implements it, but you need to specify how you'd like to sort data.
That sample illustrate how to sort list of objects by val field in descended order.
static class ToSort {
Integer val;
public ToSort(Integer val) {
this.val = val;
}
#Override
public String toString() {
return "ToSort{" +
"val=" + val +
'}';
}
}
public static void main(String[] args) {
Observable.just(new ToSort(1), new ToSort(2), new ToSort(3))
.toSortedList((o1, o2) -> (-1) * o1.val.compareTo(o2.val))
.subscribe(list -> System.out.print(Arrays.toString(list.toArray())));
}
[ToSort{val=3}, ToSort{val=2}, ToSort{val=1}]
Different approach to replce the
// comparing dates is very much dependent on your implementation
if (date <before> date2) {
return -1;
} else if (date <equal> date2) {
return 0;
} else {
return 1;
}
Using a static import java.util.Comparator.comparing;
you can do comparing using method references found inside of your date object.
Ex.
In this instance unsortedDevices is a standard ObservableList to be used in a TablView.
SortedList<HistoryDisplayItem> sortedItems = new SortedList<>(unsortedDevices,
comparingInt(HistoryDisplayItem::getNumber));
Using Joda 1.6.2 with Android
The following code hangs for about 15 seconds.
DateTime dt = new DateTime();
Originally posted this post
Android Java - Joda Date is slow in Eclipse/Emulator -
Just tried it again and its still not any better. Does anyone else have this problem or know how to fix it?
I also ran into this problem. Jon Skeet's suspicions were correct, the problem is that the time zones are being loaded really inefficiently, opening a jar file and then reading the manifest to try to get this information.
However, simply calling DateTimeZone.setProvider([custom provider instance ...]) is not sufficient because, for reasons that don't make sense to me, DateTimeZone has a static initializer where it calls getDefaultProvider().
To be completely safe, you can override this default by setting this system property before you ever call anything in the joda.
In your activity, for example, add this:
#Override
public void onCreate(Bundle savedInstanceState) {
System.setProperty("org.joda.time.DateTimeZone.Provider",
"com.your.package.FastDateTimeZoneProvider");
}
Then all you have to do is define FastDateTimeZoneProvider. I wrote the following:
package com.your.package;
public class FastDateTimeZoneProvider implements Provider {
public static final Set<String> AVAILABLE_IDS = new HashSet<String>();
static {
AVAILABLE_IDS.addAll(Arrays.asList(TimeZone.getAvailableIDs()));
}
public DateTimeZone getZone(String id) {
if (id == null) {
return DateTimeZone.UTC;
}
TimeZone tz = TimeZone.getTimeZone(id);
if (tz == null) {
return DateTimeZone.UTC;
}
int rawOffset = tz.getRawOffset();
//sub-optimal. could be improved to only create a new Date every few minutes
if (tz.inDaylightTime(new Date())) {
rawOffset += tz.getDSTSavings();
}
return DateTimeZone.forOffsetMillis(rawOffset);
}
public Set getAvailableIDs() {
return AVAILABLE_IDS;
}
}
I've tested this and it appears to work on Android SDK 2.1+ with joda version 1.6.2. It can of course be optimized further, but while profiling my app (mogwee), this decreased the DateTimeZone initialize time from ~500ms to ~18ms.
If you are using proguard to build your app, you'll have to add this line to proguard.cfg because Joda expects the class name to be exactly as you specify:
-keep class com.your.package.FastDateTimeZoneProvider
I strongly suspect it's because it's having to build the ISO chronology for the default time zone, which probably involves reading all the time zone information in.
You could verify this by calling ISOChronology.getInstance() first - time that, and then time a subsequent call to new DateTime(). I suspect it'll be fast.
Do you know which time zones are going to be relevant in your application? You may find you can make the whole thing much quicker by rebuilding Joda Time with a very much reduced time zone database. Alternatively, call DateTimeZone.setProvider() with your own implementation of Provider which doesn't do as much work.
It's worth checking whether that's actually the problem first, of course :) You may also want to try explicitly passing in the UTC time zone, which won't require reading in the time zone database... although you never know when you'll accidentally trigger a call which does require the default time zone, at which point you'll incur the same cost.
I only need UTC in my application. So, following unchek's advice, I used
System.setProperty("org.joda.time.DateTimeZone.Provider", "org.joda.time.tz.UTCProvider");
org.joda.time.tz.UTCProvider is actually used by JodaTime as the secondary backup, so I thought why not use it for primary use? So far so good. It loads fast.
The top answer provided by plowman is not reliable if you must have precise timezone computations for your dates. Here is an example of problem that can happen:
Suppose your DateTime object is set for 4:00am, one hour after daylight savings have started that day. When Joda checks the FastDateTimeZoneProvider provider before 3:00am (i.e., before daylight savings) it will get a DateTimeZone object with the wrong offset because the tz.inDaylightTime(new Date()) check will return false.
My solution was to adopt the recently published joda-time-android library. It uses the core of Joda but makes sure to load a time zone only as needed from the raw folder. Setting up is easy with gradle. In your project, extend the Application class and add the following on its onCreate():
public class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
JodaTimeAndroid.init(this);
}
}
The author wrote a blog post about it last year.
I can confirm this issue with version 1, 1.5 and 1.62 of joda. Date4J is working well for me as an alternative.
http://www.date4j.net/
I just performed the test that #"Name is carl" posted, on several devices. I must note that the test is not completely valid and the results are misleading (in that it only reflects a single instance of DateTime).
From his test, When comparing DateTime to Date, DateTime is forced to parse the String ts, where Date does not parse anything.
While the initial creation of the DateTime was accurate, it ONLY takes that much time on the very FIRST creation... every instance after that was 0ms (or very near 0ms)
To verify this, I used the following code and created 1000 new instances of DateTime on an OLD Android 2.3 device
int iterations = 1000;
long totalTime = 0;
// Test Joda Date
for (int i = 0; i < iterations; i++) {
long d1 = System.currentTimeMillis();
DateTime d = new DateTime();
long d2 = System.currentTimeMillis();
long duration = (d2 - d1);
totalTime += duration;
log.i(TAG, "datetime : " + duration);
}
log.i(TAG, "Average datetime : " + ((double) totalTime/ (double) iterations));
My results showed:
datetime : 264
datetime : 0
datetime : 0
datetime : 0
datetime : 0
datetime : 0
datetime : 0
...
datetime : 0
datetime : 0
datetime : 1
datetime : 0
...
datetime : 0
datetime : 0
datetime : 0
So, the result was that the first instance was 264ms and more than 95% of the following were 0ms (I occasionally had a 1ms, but never had a value larger than 1ms).
Hope this gives a clearer picture of the cost of using Joda.
NOTE: I was using joda-time version 2.1
Using dlew/joda-time-android gradle dependency it takes only 22.82 ms (milliseconds). So I recommend you using it instead of overriding anything.
I found solution for me. I load UTC and default time zone. So it's loads very fast. And I think in this case I need catch broadcast TIME ZONE CHANGE and reload default time zone.
public class FastDateTimeZoneProvider implements Provider {
public static final Set<String> AVAILABLE_IDS = new HashSet<String>();
static {
AVAILABLE_IDS.add("UTC");
AVAILABLE_IDS.add(TimeZone.getDefault().getID());
}
public DateTimeZone getZone(String id) {
int rawOffset = 0;
if (id == null) {
return DateTimeZone.getDefault();
}
TimeZone tz = TimeZone.getTimeZone(id);
if (tz == null) {
return DateTimeZone.getDefault();
}
rawOffset = tz.getRawOffset();
//sub-optimal. could be improved to only create a new Date every few minutes
if (tz.inDaylightTime(new Date())) {
rawOffset += tz.getDSTSavings();
}
return DateTimeZone.forOffsetMillis(rawOffset);
}
public Set getAvailableIDs() {
return AVAILABLE_IDS;
}
}
This quick note to complete the answer about date4j from #Steven
I ran a quick and dirty benchmark comparing java.util.Date, jodatime and date4j on the weakest android device I have (HTC Dream/Sapphire 2.3.5).
Details : normal build (no proguard), implementing the FastDateTimeZoneProvider for jodatime.
Here's the code:
String ts = "2010-01-19T23:59:59.123456789";
long d1 = System.currentTimeMillis();
DateTime d = new DateTime(ts);
long d2 = System.currentTimeMillis();
System.err.println("datetime : " + dateUtils.durationtoString(d2 - d1));
d1 = System.currentTimeMillis();
Date dd = new Date();
d2 = System.currentTimeMillis();
System.err.println("date : " + dateUtils.durationtoString(d2 - d1));
d1 = System.currentTimeMillis();
hirondelle.date4j.DateTime ddd = new hirondelle.date4j.DateTime(ts);
d2 = System.currentTimeMillis();
System.err.println("date4j : " + dateUtils.durationtoString(d2 - d1));
Here are the results :
debug | normal
joda : 3s (3577ms) | 0s (284ms)
date : 0s (0) | 0s (0s)
date4j : 0s (55ms) | 0s (2ms)
One last thing, the jar sizes :
jodatime 2.1 : 558 kb
date4j : 35 kb
I think I'll give date4j a try.
You could also checkout Jake Wharton's JSR-310 backport of the java.time.* packages.
This library places the timezone information as a standard Android asset and provides a custom loader for parsing it efficiently. [It] offers the standard APIs in Java 8 as a much smaller package in not only binary size and method count, but also in API size.
Thus, this solution provides a smaller binary-size library with a smaller method count footprint, combined with an efficient loader for Timezone data.
As already mentioned you could use the joda-time-android library.
Do not use FastDateTimeZoneProvider proposed by #ElijahSh and #plowman. Because it is treat DST offset as standart offset for the selected timezone. As it will give "right" results for the today and for the rest of a half of a year before the next DST transition occurs. But it will defenetly give wrong result for the day before DST transition, and for the day after next DST transition.
The right way to utilize system's timezones with JodaTime:
public class AndroidDateTimeZoneProvider implements org.joda.time.tz.Provider {
#Override
public Set<String> getAvailableIDs() {
return new HashSet<>(Arrays.asList(TimeZone.getAvailableIDs()));
}
#Override
public DateTimeZone getZone(String id) {
return id == null
? null
: id.equals("UTC")
? DateTimeZone.UTC
: Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? new AndroidNewDateTimeZone(id)
: new AndroidOldDateTimeZone(id);
}
}
Where AndroidOldDateTimeZone:
public class AndroidOldDateTimeZone extends DateTimeZone {
private final TimeZone mTz;
private final Calendar mCalendar;
private long[] mTransition;
public AndroidOldDateTimeZone(final String id) {
super(id);
mTz = TimeZone.getTimeZone(id);
mCalendar = GregorianCalendar.getInstance(mTz);
mTransition = new long[0];
try {
final Class tzClass = mTz.getClass();
final Field field = tzClass.getDeclaredField("mTransitions");
field.setAccessible(true);
final Object transitions = field.get(mTz);
if (transitions instanceof long[]) {
mTransition = (long[]) transitions;
} else if (transitions instanceof int[]) {
final int[] intArray = (int[]) transitions;
final int size = intArray.length;
mTransition = new long[size];
for (int i = 0; i < size; i++) {
mTransition[i] = intArray[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public TimeZone getTz() {
return mTz;
}
#Override
public long previousTransition(final long instant) {
if (mTransition.length == 0) {
return instant;
}
final int index = findTransitionIndex(instant, false);
if (index <= 0) {
return instant;
}
return mTransition[index - 1] * 1000;
}
#Override
public long nextTransition(final long instant) {
if (mTransition.length == 0) {
return instant;
}
final int index = findTransitionIndex(instant, true);
if (index > mTransition.length - 2) {
return instant;
}
return mTransition[index + 1] * 1000;
}
#Override
public boolean isFixed() {
return mTransition.length > 0 &&
mCalendar.getMinimum(Calendar.DST_OFFSET) == mCalendar.getMaximum(Calendar.DST_OFFSET) &&
mCalendar.getMinimum(Calendar.ZONE_OFFSET) == mCalendar.getMaximum(Calendar.ZONE_OFFSET);
}
#Override
public boolean isStandardOffset(final long instant) {
mCalendar.setTimeInMillis(instant);
return mCalendar.get(Calendar.DST_OFFSET) == 0;
}
#Override
public int getStandardOffset(final long instant) {
mCalendar.setTimeInMillis(instant);
return mCalendar.get(Calendar.ZONE_OFFSET);
}
#Override
public int getOffset(final long instant) {
return mTz.getOffset(instant);
}
#Override
public String getShortName(final long instant, final Locale locale) {
return getName(instant, locale, true);
}
#Override
public String getName(final long instant, final Locale locale) {
return getName(instant, locale, false);
}
private String getName(final long instant, final Locale locale, final boolean isShort) {
return mTz.getDisplayName(!isStandardOffset(instant),
isShort ? TimeZone.SHORT : TimeZone.LONG,
locale == null ? Locale.getDefault() : locale);
}
#Override
public String getNameKey(final long instant) {
return null;
}
#Override
public TimeZone toTimeZone() {
return (TimeZone) mTz.clone();
}
#Override
public String toString() {
return mTz.getClass().getSimpleName();
}
#Override
public boolean equals(final Object o) {
return (o instanceof AndroidOldDateTimeZone) && mTz == ((AndroidOldDateTimeZone) o).getTz();
}
#Override
public int hashCode() {
return 31 * super.hashCode() + mTz.hashCode();
}
private long roundDownMillisToSeconds(final long millis) {
return millis < 0 ? (millis - 999) / 1000 : millis / 1000;
}
private int findTransitionIndex(final long millis, final boolean isNext) {
final long seconds = roundDownMillisToSeconds(millis);
int index = isNext ? mTransition.length : -1;
for (int i = 0; i < mTransition.length; i++) {
if (mTransition[i] == seconds) {
index = i;
}
}
return index;
}
}
The AndroidNewDateTimeZone.java same as "Old" one but based on android.icu.util.TimeZone instead.
I have created a fork of Joda Time especially for this. It loads for only ~29 ms in debug mode and ~2ms in release mode. Also it has less weight as it doesn't include timezone database.