I need to get actual date with custom hour so I create this code:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
Calendar output = Calendar.getInstance();
output.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 1, 0);
output.getTime();
I hope it works but it seems a litle bit complicated.
Is there some other way how to get date with custom hour?
Is there some other way how to get date with custom hour ?
Personally I'd use Joda Time instead:
// Ideally use an injectable clock...
LocalDate today = new LocalDate();
// If this is effectively constant, extract it to a final static field
LocalTime time = new LocalTime(1, 0);
// Or use toDateTime(...) depending on what you're trying to accomplish
LocalDateTime todayAtTime = today.toLocalDateTime(time);
Joda Time has a much more pleasant API than java.util.{Date, Calendar}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 1);
cal.set(Calendar.MINUTE, 0);
cal.getTime()
what about this :
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, your_custom_hour);
your_custom_hour : 0-23
Other than use Joda time I'd do it slightly cleaner:
int hour = 1;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Calendar.getInstance() returns the current date and time anyway so setTime(new Date()) is unnecessary.
Related
I'm setting time using Timestamp class with current time. I'm setting the time with the Calendar class first by DAY_OF_WEEK and second with DAY_OF_MONTH. I’m getting the same output every time. Then what is the diffrence between DAY_OF_MONTH and DAY_OF_WEEK?
Timestamp followUpDateBegins = new Timestamp(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();
cal.setTime(followUpDateBegins);
cal.add(Calendar.DAY_OF_WEEK, -30);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
followUpDateBegins.setTime(cal.getTime().getTime());
System.out.println("followUpDateBegins "+followUpDateBegins);
OR
Timestamp followUpDateBeginsSecond = new Timestamp(System.currentTimeMillis());
cal.setTime(followUpDateBeginsSecond);
cal.add(Calendar.DAY_OF_MONTH, -30);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
followUpDateBeginsSecond.setTime(cal.getTime().getTime());
System.out.println("followUpDateBegins" + followUpDateBeginsSecond);
DAY_OF_WEEK is the day of the week (7 days), DAY_OF_MONTH is the day of the month (<=31 days)
If you use add
cal.add(Calendar.DAY_OF_WEEK, -30); is the same as cal.add(Calendar.DAY_OF_MONTH, -30);
Also DATE and DAY_OF_YEAR will act the same - because you modify Days in both cases and you just go 30 days behind.
But if your calendar is set to 15th of April for example and you do
cal.get(Calendar.DAY_OF_MONTH) and cal.get(Calendar.DAY_OF_WEEK) they will return different results.
As explained in the other answers, adding both fields results in the same date, which is a bizarre implementation detail, IMO. We can see that from the source code, there's a switch statement where both fields are considered "the same" when adding:
case DAY_OF_MONTH: // synonym of DATE
case DAY_OF_YEAR:
case DAY_OF_WEEK:
Alghough it doesn't make sense "adding days of week" (IMHO - because in the end you just add days, no matter if it's in the same week, month or whatever, and that's it), that's the way it's implemented. This API is really confusing...
Another detail is that Calendar.getInstance() already returns the current date/time - it internally calls System.currentTimeMillis(), so creating a Timestamp just to set it in the calendar is redundant. You could just do:
// create the calendar
Calendar cal = Calendar.getInstance();
// do your stuff
...
// create the timestamp
Timestamp followUpDateBeginsSecond = new Timestamp(cal.getTimeInMillis());
I want to construct a date based in a java.util.date and a java.sql.Time, so I code this:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY,time.getHours());
cal.set(Calendar.MINUTE, time.getMinutes());
cal.set(Calendar.SECOND, time.getSeconds());
cal.getTime();
It works but ime.getHours(), time.getMinutes(), time.getSeconds() appears as deprecated, how can we make it with a no deprecated method ????
By the looks of what you're doing, I think this is what you want:
Calendar mergedCal = Calendar.getInstance();
mergedCal.setTime(date);
Calendar sqlCal = Calendar.getInstance();
sqlCal.setTime(time.getTime());
mergedCal.set(Calendar.HOUR_OF_DAY, sqlCal.get(Calendar.HOUR_OF_DAY));
mergedCal.set(Calendar.MINUTE, sqlCal.get(Calendar.MINUTE));
mergedCal.set(Calendar.SECOND, sqlCal.get(Calendar.SECOND));
mergedCal.getTime();
At the end of this computation, mergedCal will use the date from date and the time-of-day from time.
I want to reset java.util.Date to the beginning of the day
using
Date date = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
with
Date date = new Date();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String s=df.format(date);
try {
date = df.parse(s);
} catch (ParseException ex) {
}
Which one is better?
This one's better in terms of clarity and readability, though both gives same output.
Date date = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
With help of Apache Commons org.apache.commons.lang3.time.DateUtils you can also get the desired result like this:
DateUtils.truncate(new java.util.Date(), java.util.Calendar.DATE));
First solution (manipulation via Calendar) is definitly better. The second solution is just a rather ugly workaround using string-manipulation, that will most likely be slower than the first one.
You can for first approach and you need not set time in Calendar.
// Date date = new Date();
Calendar cal = new GregorianCalendar();
// cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();
Given the assignment t = System.currentTimeMillis(), accrued at some point in the past, how do I get the millis of the same day as t in 12 pm and the day after 12 pm?
Note: this is timezone dependent. You can do as such:
import static java.util.Calendar.*;
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(yourValue);
cal.set(HOUR_OF_DAY, 12);
cal.set(MINUTE, 0);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
// cal.getTimeInMillis() contains the wanted day at 12pm
cal.add(DAY_OF_YEAR, 1);
// cal.getTimeInMillis() now contains the wanted day plus one at 12pm
But do yourself a favour and use Joda Time, it is much easier to use in this case:
final DateTime dayAt12pm = new DateTime(yourValue).toDateMidnight()
.plusHours(12);
// dayAt12pm.getMillis() contains the wanted day at 12pm
// next day at 12pm: dayAt12pm.plusDays(1).getMillis()
//plug your "T" here.
long t = System.currentTimeMillis();
Date date = new Date(t);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar startOfDay = Calendar.getInstance();
startOfDay.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
startOfDay.set(Calendar.HOUR, 12);
startOfDay.set(Calendar.MINUTE, 0);
startOfDay.set(Calendar.SECOND, 0);
startOfDay.set(Calendar.MILLISECOND, 0);
Date startOfDayDate = startOfDay.getTime();
System.err.println("12PM on time t is " + startOfDayDate);
startOfDay.add(Calendar.HOUR, 24);
startOfDayDate = startOfDay.getTime();
System.err.println("12PM day after t is " + startOfDayDate);
One way would be to create a Date from it and then use the Calendar class.
I get the today's date like this:
final Calendar cal = Calendar.getInstance();
{
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
}
I want to calculate what was the date x days ago... anyone got something?
A better way would be to use add method instead of set:
cal.add(DAY_OF_YEAR, -2);
I.e. to be sure it works also the first day in month etc.
You can do the following :
Calendar cal=Calendar.getInstance();
int currentDay=cal.get(Calendar.DAY_OF_MONTH);
//Set the date to 2 days ago
cal.set(Calendar.DAY_OF_MONTH, currentDay-2);
then you can get the date :
cal.getTime(); //The date 2 days ago
I use the following fuction:
public static Date getStartOfDay() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static long getDaysAgo(Date date){
final long diff = getStartOfDay().getTime() - date.getTime();
if(diff < 0){
// if the input date millisecond > today's 12:00am millisecond it is today
// (this won't work if you input tomorrow)
return 0;
}else{
return TimeUnit.MILLISECONDS.toDays(diff)+1;
}
}
Same kind of code, but using the Joda-Time 2.3 library and Java 7.
DateTime dateTime = new DateTime( 2014, 2, 3, 7, 8, 9 );
DateTime twoDaysPrior = dateTime.minusDays( 2 );
dateTime: 2014-02-03T07:08:09.000-08:00
twoDaysPrior: 2014-02-01T07:08:09.000-08:00