How one could get Chinese New Year Date on Android?
Since API level 24 Android has Chinese Calendar class.
https://developer.android.com/reference/android/icu/util/ChineseCalendar
However, doing it like this returns wrong date (Feb 12 for 2023).
val chinese = ChineseCalendar.getInstance()
chinese.set(ChineseCalendar.MONTH, 0)
chinese.set(ChineseCalendar.DAY_OF_MONTH, 1)
I was able to get Gregorian date for Chinese new year in the following way. Getting Chinese calendar is done using simple instantiation ChineseCalendar(). No need to call getInstance().
val chinese = ChineseCalendar()
chinese.set(ChineseCalendar.MONTH, 0)
chinese.set(ChineseCalendar.DAY_OF_MONTH, 1)
println("chinese " + chinese.time.toString())
In the logs I got
chinese Sun Jan 22 13:24:41 GMT+02:00 2023
You can also add year to get next new year date, like this
chinese.add(ChineseCalendar.YEAR, 1)
and get
chinese Sat Feb 10 13:27:41 GMT+02:00 2024
Related
I am trying to add one day to calendar date but i am getting wrong output.
Below code i am using.
var cal2= Calendar.getInstance()
cal2!!.timeInMillis=cal.timeInMillis
Log.e("Time1",""+cal.timeInMillis);
cal2.add(Calendar.DATE, 1)
Log.e("Time2",""+cal2.timeInMillis);
Time1: 1526478465( Wednesday, 16 May 2018 19:17:45)
Time1: 1612878465(Tuesday, 9 February 2021 19:17:45 )
Assuming your cal.timeInMillis has proper value (say today's date), your code works fine:
var cal = Calendar.getInstance() <-- Assumption
var cal2 = Calendar.getInstance()
cal2!!.timeInMillis=cal.timeInMillis
println("Time1: "+cal.timeInMillis);
cal2.add(Calendar.DATE, 1)
println("Time2: "+cal2.timeInMillis);
Running above code gives following output:
Time1: 1527159971747 (Thursday, May 24, 2018 11:06:11.747 AM)
Time2: 1527246371747 (Friday, May 25, 2018 11:06:11.747 AM)
You’re somehow confusing seconds and milliseconds since the epoch. 1 526 478 465 is seconds. If you treat them as milliseconds, you get January 18, 1970 4:01:18 PM UTC. If you add 1 day to that, you get the next value you mention (I got 1 612 878 000, it comes close). When in turn you interpret 1 612 878 465 as seconds, you get February 9, 2021 1:47:45 PM UTC. This is the same as the date-time you mention, Tuesday, 9 February 2021 19:17:45, if I assume you’re at offset +05:30 (like Asia/Kolkata or Asia/Colombo time zone).
Since there are 1000 milliseconds in a second, your confusion has caused you to add 1000 days to your date instead of 1 day.
Tip: 10 digit values are usually seoncds. 13 digit values are usually milliseconds.
This is really driving me crazy. The code below
DateTime dt = new DateTime()
.withYear(2014)
.withWeekOfWeekyear(52)
.withDayOfWeek(1);
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("d MMM yyyy");
String firstDayOfWeek = dateTimeFormatter.print(dt);
Log.v(FILE_NAME,"display date? " + firstDayOfWeek);
dt = new DateTime()
.withYear(2015)
.withWeekOfWeekyear(52)
.withDayOfWeek(1);
String lastDayOfWeek = dateTimeFormatter.print(dt);
Log.v(FILE_NAME,"display date? " + lastDayOfWeek);
dt = new DateTime()
.withYear(2016)
.withWeekOfWeekyear(52)
.withDayOfWeek(1);
lastDayOfWeek = dateTimeFormatter.print(dt);
Log.v(FILE_NAME,"display date? " + lastDayOfWeek);
Somehow will always output:
display date? 22 Dec 2014
display date? 21 Dec 2015
display date? 21 Dec 2015
As you can see, the last display date should display 2016, not 2015. It seems that everytime I set withYear to 2016, it will magically change to 2015. Is this a bug or am I doing something wrong? I have cleaned and rebuild my project many times but the output is the same.
The method withYear(...) does not do what you think because it uses the standard calendar year and not the year of week date as described in ISO-8601-paper. Please compare following two snippets. Only the second one does what you need:
DateTime dt = new DateTime().withYear(2016).withWeekOfWeekyear(52).withDayOfWeek(1);
System.out.println("joda=" + dt); // joda=2015-12-21T18:26:12.776+01:00
DateTime dt2 =
new DateTime().withWeekyear(2016).withWeekOfWeekyear(52).withDayOfWeek(1);
System.out.println("joda=" + dt2); // joda=2016-12-26T18:27:59.606+01:00
See also the documentation. The fine difference between calendar year and weekbased year is only noticeable at the end or start of a year (like today).
Explained in detail the behaviour:
If choosing new DateTime() for today, the second of January 2017 and then setting the calendar year to 2016 results in: 2016-01-02. But this date is in week-of-year 53 belonging to week-based-year 2015. This 53rd week starts on 2015-12-28, so the expression withWeekOfWeekyear(52) will go back one week to 2015-12-21 (what you observe in first case).
I have an Asus Fonepad first generation(android 4.1.2) and an Asus Fonepad second generation(android 4.3).
I use the following code to parse the date to the desired format in my app:
SimpleDateFormat df = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", /*new Locale("nl", "NL")*/Locale.GERMANY);
df.setTimeZone(TimeZone.getTimeZone(/*"Europe/Amsterdam"*/"Europe/Berlin"));
String time = df.format(new Date());
The above code results in "Dez 23 17:09:25 2013" on the first generation fonepad and "Dez. 23 17:09:25 2013" on the second generation fonepad.
As you can see, the second generation adds a dot after the month.
this causes a parsexception on the server side.
Why does SimpleDateFormat behave differently on different devices(android versions)? This is worrying.
Is there a way to always get the same format? What is the solution to this?
Thanks.
SimpleDateFormat (and some other framework classes) use icu4c library to format content. Month format for DE was changed between 49.2 and 50.1 versions of this library.
No, you can't expect same behavior for all android versions.
Link to sources: https://android.googlesource.com/platform/external/icu4c/+/android-4.4.2_r1/data/locales/de.txt
Add: If you sending data to a server than solution is to use only numbers: 12 will be always 12 for December.
I have two devices HTC with android 2.3.5 and Samsung with 2.3.6
now the problem i am facing is
i need the date's week of month.
So i've written this code and installed on both the phones. and set the system date as
27th Jan 2013
Calendar calendar = Calendar.getInstance();
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
Log.i(TAG,"weekOfMonth = "+weekOfMonth);
now on HTC the output is
weekOfMonth = 5
while on samsung running the same code produces
weekOfMonth = 4
this really is screwing my logic n calculations ahead.
am i doing something wrong ?
It is probably due to Locales. In Java
Calendar calFr = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
Calendar calUs = Calendar.getInstance(TimeZone.getTimeZone("US/Eastern"), Locale.US);
Calendar calUk = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.UK);
calFr.set(2013, Calendar.JANUARY, 27);
calUs.set(2013, Calendar.JANUARY, 27);
calUk.set(2013, Calendar.JANUARY, 27);
int weekOfMonthFr = calFr.get(Calendar.WEEK_OF_MONTH);
int weekOfMonthUs = calUs.get(Calendar.WEEK_OF_MONTH);
int weekOfMonthUk = calUk.get(Calendar.WEEK_OF_MONTH);
System.out.println("France week of month is " + weekOfMonthFr);
System.out.println("USA week of month is " + weekOfMonthUs);
System.out.println("UK week of month is " + weekOfMonthUk);
will give you
France week of month is 4
USA week of month is 5
UK week of month is 4
Double check the year that it is set to, if one is set to 2012 and the other 2013, that would explain the difference.
Can you get the applications to log the date held in the calendar object and post it here as well just to make sure they have correct information.
I can"t get to work a simple code in a fresh project that is suppose to simply write one date in many langage depending on the Locale set.
Locale[] locales = new Locale[] {
Locale.JAPAN,
Locale.CHINA,
Locale.KOREA,
Locale.TAIWAN,
Locale.ITALY,
Locale.FRANCE,
Locale.GERMAN
};
// Get an instance of current date time
Date today = new Date();
//
// Iterates the entire Locale defined above and create a long
// formatted date using the SimpleDateFormat.getDateInstance()
// with the format, the Locale and the date information.
//
for (Locale locale : locales) {
System.out.println("Date format in "
+ locale.getDisplayName()
+ " = "
+ SimpleDateFormat.getDateInstance(
SimpleDateFormat.LONG, locale)
.format(today).toUpperCase());
}
}
Here is the link of this code : URL of the code below
Now here is what it's suppose to display
Date format in Japanese (Japan) = 2009/01/04
Date format in Chinese (China) = 2009年1月4日
Date format in Korean (South Korea) = 2009년 1월 4일 (일)
Date format in Chinese (Taiwan) = 2009年1月4日
Date format in Italian (Italy) = 4 GENNAIO 2009
Date format in French (France) = 4 JANVIER 2009
Date format in German = 4. JANUAR 2009
And here is MY display :
Date format in Japanese (Japan) = 2012 7 21
Date format in Chinese (China) = 2012 7 21
Date format in Korean (South Korea) = 2012 7 21
Date format in Chinese (Taiwan) = 2012 7 21
Date format in Italian (Italy) = 2012 7 21
Date format in French (France) = 2012 7 21
Date format in German = 2012 7 21
PROBLEM : What's wrong ? Am I forgeting some obvious thing ? Do you have any lead ?
Thanks.
As it turned out it's a device-specific problem. The code works on a phone with correct locale data and also on emulator. On HTC Desire there are some ROMs which have locale data corrupted. More on this issue.