How to get year, month,day from given timestamp like "1459233289187" [duplicate] - java

This question already has answers here:
How to get year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java?
(10 answers)
Closed 6 years ago.
I am calculating timestamp from system time as
System.currentTimeMillis()
in java.
Later, i want to calculate year , moth and date from this time stamp.
is there any way to do so?

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
int year = cal.get(Calendar.YEAR);

Related

Subtract java.util.Date with Integer [duplicate]

This question already has answers here:
WHERE datetime older than some time (eg. 15 minutes)
(2 answers)
Adding/Subtracting 5 seconds from Java Date - Showing deprected warning
(9 answers)
Changing Java Date one hour back
(11 answers)
Closed 2 years ago.
I need to delete some records from the database. In the table, I have a column with Date (java.util.Date) records. I need to get from the table the rows that are older than x hours for example. My function has an int Y that deserves seconds. I need to make something like that. Date (currentDate) - Y. And after that to make a query where I compare that column with the calculation. Any suggestion?
First, if you work with java 8 or higher I would strongly advise not to use outdated class java.util.Date and switch to package java.time. You can use class ZonedDateTime or any of its "brothers" such as LocalDateTime or others.
But, to answer your question here is how you can substruct seconds from Date class:
Date date = new Date(); //Current date
long seconds = 100L; // 100 seconds to substruct
date.setTime(date.getTime() - seconds * 1000); //convert seconds to milliseconds and substruct it from current value of date
I solved this with this function. I hope will be helpful for somebody.
private Date calculateTheDate(final int seconds){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, - seconds);
return calendar.getTime();
}

How to get last month previous date from current date in java [duplicate]

This question already has answers here:
How to get last month/year in java?
(10 answers)
Closed 5 years ago.
Presently i want to display last one month records based on current date. So i need last month,last date & last year. In between months i need 30 days. For Ex: current date is 12/12/2017 (dd/mm/yyyy) then expected date is 13/11/2017
EX: current date is 12/03/2017 then expected date is 11/02/2017
You can simply do it using calendar.
Calendar c=Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_YEAR, -30);
System.out.println(c.getTime());
This should solve your problem.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.add(Calendar.DATE, -1);

A bug in week of year in Java Calendar? [duplicate]

This question already has answers here:
Understanding java.util.Calendar WEEK_OF_YEAR [duplicate]
(2 answers)
Closed 7 years ago.
The following code calculates the workweek of a specific date.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = new GregorianCalendar();
cal.setTime(df.parse("2015-12-27 08:00:00"));
System.err.printf("%d.%02d\n", cal.getWeekYear(), cal.get(Calendar.WEEK_OF_YEAR));
It currently prints 2016.01.
As I understand the work week number specification, 2016.01 is the first week having 4 days in 2016, but there is no way December 27 can belong to such week.
Is there a way to do it in Java 7 which will work for any year assuming weeks start on Monday?
Try setting Monday as first day of the week.
cal.setFirstDayOfWeek(Calendar.MONDAY);

Getting hours between 2 timezones [duplicate]

This question already has answers here:
Android how to get time difference between two time zones in android?
(4 answers)
Rails: Difference between timezones in hours
(1 answer)
Closed 8 years ago.
I'd like to get the number of hours between 2 different time-zones. I'm currently using Joda time but it looks like it's taking daylight savings into account because it's off by an hour. The only correct one seems to be my current timezone where it returns 0 hours but if I use London for instance which for me is 5 hr difference it returns 4.
Current code:
DateTime endTime = new DateTime(date);
DateTime startTime = new DateTime();
Period period = new Period(startTime, endTime);
int hours = period.getHours();

How to get the 30 days back date in java based on given date [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to subtract X day from a Date object in Java?
(10 answers)
Closed 9 years ago.
I have an input field, where i can select the date from date picker.Based on this selected value i need to get the 30 days back date.
Thanks in advance.
Calendar cal = Calendar.getInstance();
cale.add(Calendar.DATE, -30);
System.out.println("Date = " + cal.getTime());

Categories

Resources