Getting Year, Month and Date in Android
Would I be correct in assuming this is running on a Emulator? If so, Set the emulator date correctly, and it should be correct. From memory, that code should do what you expect.
Would I be correct in assuming this is running on a Emulator? If so, Set the emulator date correctly, and it should be correct. From memory, that code should do what you expect.
A Calendar can’t be directly formatted, you need to get the Date from the Calendar, like this: String formattedDate = dateFormat.format(someDate.getTime());
Calendar c1 = Calendar.getInstance(); // today c1.add(Calendar.DAY_OF_YEAR, -1); // yesterday Calendar c2 = Calendar.getInstance(); c2.setTime(getDateFromLine(line)); // your date if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR)) { This will also work for dates like 1st of January.
I started by implementing some temporal expression as outlined by Martin Fowler. This takes care of figuring out when a scheduled item should actually occur. It is a very elegant way of doing it. What I ended up with was just a build up on what is in the article. The next problem was figuring … Read more
In Java, Dates are internally represented in UTC milliseconds since the epoch (so timezones are not taken into account, that’s why you get the same results, as getTime() gives you the mentioned milliseconds). In your solution: Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone(“GMT”)); long gmtTime = cSchedStartCal.getTime().getTime(); long timezoneAlteredTime = gmtTime + TimeZone.getTimeZone(“Asia/Calcutta”).getRawOffset(); Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone(“Asia/Calcutta”)); cSchedStartCal1.setTimeInMillis(timezoneAlteredTime); … Read more
Date has no timezone and internally stores in UTC. Only when a date is formatted is the timezone correction applies. When using a DateFormat, it defaults to the timezone of the JVM it’s running in. Use setTimeZone to change it as necessary. DateFormat utcFormat = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”); utcFormat.setTimeZone(TimeZone.getTimeZone(“UTC”)); Date date = utcFormat.parse(“2012-08-15T22:56:02.038Z”); DateFormat pstFormat = … Read more
Your solution is here but instead of addition you need to use subtraction c.add(Calendar.MONTH, -1); Then you can call getter on the Calendar to acquire proper fields int month = c.get(Calendar.MONTH) + 1; // beware of month indexing from zero int year = c.get(Calendar.YEAR);
Did you check DateTime-Indic-0.1 family of modules? At least DateTime::Indic::Chandramana seems to have a method to convert traditional date into UTC values (utc_rd_values). UPDATE: I suppose Calendar::Saka may be useful as well for many users (as I have known, it’s the Indian national calendar), in particular, to_gregorian() and from_gregorian() methods.
You can use date from the datetime module to find the first Sunday in a year and then keep adding seven days, generating new Sundays: from datetime import date, timedelta def allsundays(year): d = date(year, 1, 1) # January 1st d += timedelta(days = 6 – d.weekday()) # First Sunday while d.year == year: yield … Read more
Since Java 8, the quarter is accessible as a field using classes in the java.time package. import java.time.LocalDate; import java.time.temporal.IsoFields; LocalDate myLocal = LocalDate.now(); quarter = myLocal.get(IsoFields.QUARTER_OF_YEAR); In older versions of Java, you could use: import java.util.Date; Date myDate = new Date(); int quarter = (myDate.getMonth() / 3) + 1; Be warned, though that getMonth … Read more