How to check if a date Object equals yesterday?

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.

How to change TIMEZONE for a java.util.Calendar/Date

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

Java: How do you convert a UTC timestamp to local time?

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

Converting to and from Hindu calendar

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.

How do I discover the Quarter of a given Date?

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)