Tweaked formating of date with days of week, and mounts - android

Well instead of 2011.10.19 10:30 I want to have something like 10:30, Wednesday or instead of 2011.09.19 10:30 I want to have something like 10:30, September. I know that I can code this somehow but I want clean and not greedy solution. The code will run on android devices so I need something that acts gracefully with resources and something that is well written. PreatyTime doesn't helps me cause it calculates distance from this moment , and there is no formatting like 12:00, Monday.
Does someone know some good pattern that I can follow when writing this kind of things ?

have a look at this
It will give you any format you want,by just passing a Calendar object.
For example:
Calendar cal=Calendar.getInstance();
String myFormat=String.format("%1$tH:%1$tM %1$tB",cal);
would give you format like "10:30 September".

Related

How to create an own Calendar-structure?

Community,
Basically I'm working on my own Calendar App. Its a private Project to add specific functions and I wont use for example s-planner, Calendar and so on.
The Google Calendar is a great choice in my opinion but I want to learn the basics by myself.
I just started a few days ago and here we go:
The App needs 4.4 Kitkat and above versions. I started with a basic calendar view, looking like this:
So it should be possible to add Events and Reminders. It is possible to check which date is selected and working with a Date Tim Picker dialog? Or should I create an GridView and fill it. Maybe that way:
Just use the calendar api and create an Instance like this:
Calendar calendar = GregorianCalendar.getInstance();
But there is for example, no function which gives me the Dates of a Month in a Array . How its possible to fill it.
I just looked for some examples :
https://www.toptal.com/android/android-customization-how-to-build-a-ui-component-that-does-what-you-want
Is this necessary? Maybe there is an easy way. I don't understand the Calendar Api.
The Logic behind the calendar could be organized with the CalendarContract.
For example:
Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(calIntent);
So that's not the case now.
I just want to know how I can create the calendar structure and display it. Maybe a hint which way is possible to display dates of another Month ( for example: the first November 2016 = Tuesday so i should display the 31.10 too)
I guess there are a lot of information that I miss at this time. Hope some of you can help me. I'm sorry for my English, it isn't the best but you should understand the point. Have a great Day.
Its also my first post here, so let me know which information is missing or what I should reformat.
Are you looking for something like this. This is a fully customised calendar where you can add events to dates , can listen to date change etc :
https://github.com/khetanrajesh/CustomCalendar

Getting time in Time format

I am currently trying to work with Location updating.
I've been using the below to work out the time-stamps of when the last update was processed:
String NewLocationTime = DateFormat.getTimeInstance().format(new Date());
However, to work out the difference between two times, I needed to parse it to a Time format. However, for some reason the Android Studio didn't recognize the DateTimeFormatter, and because I don't know in what format the String is going to come up, I am not quite sure what to put in the formatting either.
I believe it would be much easier if I was to be able to get a Time object straight away, so I can use something like:
long diffInMinutes = java.time.Duration.between(dateTime1, dateTime2).toMinutes();
Can anyone tell me how I get the Time object straight away, or why the Android Studio does not recognize the DateTimeFormatter?
Thank you
Use new Date().getTime() and what you get is the time in milliseconds and you can work with it as you want.
You need to use Date and use Date.getTime() for difference calculation. I do though recommend Joda-Time.
Also look at Calendar.
Time an sql aware wrapper around Date and there is no such thing as DateTime or DateTimeFormatter.

android: timezone offset for "Europe/Russia" [duplicate]

This question already has an answer here:
android timezone difference is 1 hour less then expected
(1 answer)
Closed 9 years ago.
I need an offset for "Europe/Russia" to UTC.. in hours. here is my code:
Calendar mCalendar = new GregorianCalendar();
mCalendar.setTimeZone( TimeZone.getTimeZone("Europe/Moscow"));
TimeZone mTimeZone = mCalendar.getTimeZone();
int remote_offset = mTimeZone.getRawOffset()/1000/60/60;
For UTC it should be -4 hours. BUT! some user got 3 hours difference!!
I think, the problem is, Russia doesn't use winter time. And some devices now that, but some not.. how could I implement allway to get "-4" hours?
Regards
First, Russia isn't UTC-4.
The problem has to do with Russia not having daylight saving time. But your issue is probably only happening with android 2.x device and less. The daylight saving time was removed before 4.x as far as I remember (if it's a user input). On the other hand, if you receive a date that was created by the device without user input, you don't have to convert it as it's already as UTC.
But as I said, Russia isn't -4. Russia/Moscow will be +4 hours. But Russia is larger than Moscow really!
Look here: http://en.wikipedia.org/wiki/Time_in_Russia
UTC+03:00 MSK−1: Kaliningrad Time Europe/Kaliningrad
UTC+04:00 MSK: Moscow Time Europe/Moscow, Europe/Volgograd, Europe/Samara
UTC+06:00 MSK+2: Yekaterinburg Time Asia/Yekaterinburg
UTC+07:00 MSK+3: Omsk Time Asia/Omsk, Asia/Novosibirsk, Asia/Novokuznetsk
UTC+08:00 MSK+4: Krasnoyarsk Time Asia/Krasnoyarsk
UTC+09:00 MSK+5: Irkutsk Time Asia/Irkutsk
UTC+10:00 MSK+6: Yakutsk Time Asia/Yakutsk
UTC+11:00 MSK+7: Vladivostok Time Asia/Vladivostok, Asia/Sakhalin
UTC+12:00 MSK+8: Magadan Time Asia/Magadan, Asia/Kamchatka, Asia/Anadyr
So what you'll have to do is to check if we're in winter and that the TimeZone is one of those. If the timezone is one of those, you can add one more hour when you want to show. And remove 1 hour when you want to convert to UTC.
I don't believe it's possible to update the TimeZone on the android phones and that also means that it's not exactly possible to do that unless you find an alternative library for Dates that has timezones built-in and which are updated.
You could subclass the DateObject with the functions that you use to behave just like the old date object, all you'll have to do is to make sure it behaves differently on android2.x and not on android 4.x+.
Also check this: http://www.joda.org/joda-time/
I checked there and I guess it could be usable and less hacky than my suggestion above. The TimeZones are up to date so it could just work for every phone since it shouldn't use the internal timezones. On the other hand, if you have functions that require the Date, it might get tricky.
My suggestion is make sure you use UTC everywhere and use JodaTime to format the date with timezones and to do "datetime" operations. If you make sure that your Java Date never contain a TimeZone other than UTC. It should work.

Using a DatePicker in Android dev: How do I compare the date picked to today's date?

I'm really stuck with a certain problem and I'm hoping someone can help me understand the problem and come to a solution. I've looked online a fair bit but can't see an answer unless it's been staring me in the face :-/
Basically, I'm creating a very basic TV Guide app. It parses data from an RSS feed which has days offset (yesterday was -1. today is 0, tomorrow is 1, etc etc) and I'm trying to implement a DatePicker that allows the user to see what is on a particular channel when they select yesterday, today, tomorrow, etc.. but if they pick a date that is out-with the range (at the moment it's a week in advance), a simple Toast message will be displayed.
My questions I guess are, firstly, how do I use maybe an IF ELSE to either parse the specific channel data for the day the user wants or display an error Toast message, and, how do I go about converting the days from what the user has put in compared to the actual date today into integers? If they select yesterday's date it will go to URL "http://example.com/-1/channel", if they select tomorrow's date it will go to URL "http://example.com/1/channel" etc etc etc.
Code is available if anyone needs to see it, but I think if someone would be kind enough to explain the logic, I'd like to see if I can come to the answer myself...
Thanks a lot folks!!
You should use a DatePicker to allow the user to choose the when.
Time in Android is stored on a long (not an int). And the long time can easily be converted back and forth between long (always milli-seconds) and a Date object.
The Date object gives you all sorts of tools to compare before and after, look at months, minutes, hours, etc.
The current time is determined by:
long nowMs = System.currentTimeMillis();
int nowSec = (int)(nowMs / 1000);
There is also a very important Calendar object. This allows you to parse textual date formats as delivered by your http functions in and out of various dates.
For example:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss Z");
String text = sdf.format(cal.getTime();
You will have to put all these tools together with a DatePicker example such as the one here Create a DatePicker to complete your TV Guide application.
Reference:
Date
Calendar
DatePicker
EDIT : Check David's Answer its better.
First Filter the date selected with today's date. You can compare it by date.isbefore(date) or date.isafter(date) these booleans will let you tell know if a date is of past or future or present. then to further calculate the days inbetween you can make a method with switch statement that will basically convert the selected date and the current date into miliseconds(Date.getTimeinmiliseconds)
if the date is of past take the difference of present time in miliseconds and past date in miliseconds. If the date is of future do the opposite. Take the difference and convert it to days difference with appropriate sign(negative/positive).
Please refer this link for a better coding example

Android Localization: What am I doing wrong that I can get months be displayed according to the locale?

This is very simple, I would expect the following piece of code to display Gennaio (Italian for January) but it still displays January. Even if I set the device locale to Italy. Any help will be appreciated. Thanks,W.
Calendar calendar= Calendar.getInstance(Locale.ITALIAN);
calendar.set(2011,0,1);
button.setText((calendar.getTime().toString()));
Your conclusion is logical but toString() is usually and certainly in this case a utility function, meant mainly for debugging.
For localised dates you need to use a DateFormatter, like so:
Calendar calendar=Calendar.getInstance();
calendar.set(2011,0,1);
String formatted=DateFormat.getLongDateFormat(this).format(calendar.getTime());
button.setText(formatted);
This example uses one of the three standard date formatters but you can get quite specific about the format, particularly be calling DateFormat.getInstance(context).getDateInstance which allows you to set more parameters, including the locale.
There's a litte more detail here: http://developer.android.com/reference/java/text/DateFormat.html
I hope that's what you were looking for.

Categories

Resources