Composing and comparing a date in Java - java

I used this to create a date and put it into a database:
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
I want to build a date with a similar format to the one above and compare it with currentDateTimeString.
I have 3 integers. How do I do that (int year, int month, int day)?
UPDATE:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);
Date result = cal.getTime();
String currentDateTimeString2 = DateFormat.getDateInstance().format(result);
I do here something wrong..both arent equal:
currentDateTimeString2==currentDateTimeString //false

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, date);
cal.set(Calendar.MONTH, month-1);//it starts from 0
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date result = cal.getTime();
I do here something wrong..both arent equal: currentDateTimeString2==currentDateTimeString //false
String are object, it can't be compared with == use equals() method

Dates are represented in milliseconds. new Date returns the current date, and by date it means year, month, day, hour, minute, second, millisecond.
Comparison can be made via Date.isAfter.
Don't use Strings to compare Dates. Don't use == to compare Objects.

Related

Set Calendar Date to Current Date

I have variable with value of timeInMills which is past 3 days ago, I wanted to reset the date of it to current date but the time should be still.
Calendar calNow = Calendar.getInstance();
Calendar calSets = (Calendar)calNow.clone();
calSets.setTimeInMillis(TIME_IN_MILL); //set datetime from timeInMillis
//Reset the date to current Date.
How to do that?
Like this, get the properties you want, before you change the instance:
Calendar calNow = Calendar.getInstance();
Calendar calSets = (Calendar)calNow.clone();
int hours = calNow.get(Calendar.HOUR_OF_DAY)
int minutes = calNow.get(Calendar.MINUTE)
calSets.setTimeInMillis(TIME_IN_MILL); //set datetime from timeInMillis
//Reset the date to current Date.
calSets.set(Calendar.SECOND, 0);
calSets.set(Calendar.MILLISECOND, 0);
calSets.set(Calendar.HOUR_OF_DAY, hours);
calSets.set(Calendar.MINUTE, minutes);
You can reset a Calendar by calling setTimeInMillis(System.currentTimeMillis()):
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // Just for testing
final long TIME_IN_MILL = 1563204600000L; // 2019-07-15 15:30 UTC
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(TIME_IN_MILL);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
cal.setTimeInMillis(System.currentTimeMillis()); // Reset
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(cal.getTime()));
The code prints 2019-07-18 15:30:00.000, which is todays date with the time of day from the TIME_IN_MILL value.
If you don't want to rely on System.currentTimeMillis(), just get the value from the Calendar object, first thing:
Calendar cal = Calendar.getInstance();
long now = cal.getTimeInMillis();
cal.setTimeInMillis(TIME_IN_MILL);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
cal.setTimeInMillis(now);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

Calculate time difference between current and future time

I want to calculate time difference in milliseconds from current time of a day(11 am , 1 october,2012) and time at midnight for the same day (11 pm 59 m 59s , 1 october , 2012.
I have tried this
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 59);
cal.add(Calendar.HOUR, 23);
cal.add(Calendar.MINUTE, 59);
cal.getTime().getTime() - today.getTime();
here today is the current date.
But when i print long values of cal and today , the time difference if of 86400 approx one day.
Use cal.set() instead of cal.add()
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
long diff = cal.getTime().getTime() - today.getTime();
You can set your date to newly created Calendar instance..
And then compare it with current instance using getTimeInMillis()
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.DATE, 1);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.YEAR, 2012);
long difference = cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();

How in Java find dates for previous 2 mondays?

May be like this:
for(int i=0;i<15;i++){
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, -1);
if (cal.Calendar.DAY_OF_WEEK==1){
System.out.println(cal.cal.getTime())
But may be exists more simple way?
Thanks.
You are on the right track.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -7); // First week before
cal.add(Calendar.DAY_OF_YEAR, -7); // Second week before
Let me make this work for just Mondays.
Calendar cal = Calendar.getInstance();
int weekday = cal.get(Calendar.DAY_OF_WEEK);
int days = (Calendar.SATURDAY - weekday + 2) % 7;
cal.add(Calendar.DAY_OF_YEAR, days);
cal.add(Calendar.DAY_OF_MONTH, -7);
cal.add(Calendar.DAY_OF_MONTH, -7);
Even simpler would be to set the weekday directly:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DATE, -7);
System.out.println(cal.getTime());
Please keep in mind, that this does not effect the time. If you want 00:00, you need to set the appropriate values:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
java.time
Use a TemporalAdjuster.
LocalDate today = LocalDate.now();
LocalDate previousOrSameMonday = today.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) );
And subtract a week to get a second one.
LocalDate secondMondayBefore = previousOrSameMonday.minusWeeks( 1 );

Date text box should be always 1st date current month.?

I want a javascript or java program should always give date 1st of current month.
Is there any tech?
You can use Calendar for Java
Date date = new Date(System.currentTimeMillis());
cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
Now you do what every you want to do with this Calendar object like to get the Day of the Week (Sat, Sun, .... )
int weekday = cal.get(Calendar.DAY_OF_WEEK);
And for JavaScript you can use:
var theFirst = new Date();
theFirst.setDate(1);
setDate sets the day of the month for the Date object (from 1 to 31). Then you can do whatever you want with theFirst, like get the day of the week.
Calendar ans = Calendar.getInstance();
ans.set(ans.get(Calendar.YEAR),
ans.get(Calendar.MONTH),
1,
0,
0,
0
);
System.out.println(ans.getTime());

java issue with date

I have a problem in java with sql date. I have the value of year-y, month-m,day-d I create new Date(y,m,d) but it says that this constructor is deprecated and it returns wrong date,
how can I correctly set date, having y,m,d ?
According to the API,
As of JDK version 1.1, replaced by
Calendar.set(year + 1900, month, date)
or GregorianCalendar(year + 1900,
month, date).
You should use public Date(long date) where date is the number of milliseconds since January 1, 1970, 00:00:00 GMT
In your case you can also use:
Date mydate = Date.valueOf("2011-05-04"); // yyyy-mm-dd
Here's how you can convert between the two:
new java.util.Date(sqlDate.getTime());
Calendar c = Calendar.getInstance();
c.set(year, month, date);
Date d = c.getTime();
Use Calendar.
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, d);
c.set(Calendar.MONTH, m);
c.set(Calendar.YEAR, y);
Then if you have to use dates, then you can do the following -
Date d = c.getTime();
Use the Calendar class:
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());

Categories