is there a java (7) equivalent of the .Net timespan?
or should i compare dates? i'm not sure how to subtract 1 time/date from another + get a totalminutes answer
Printable View
is there a java (7) equivalent of the .Net timespan?
or should i compare dates? i'm not sure how to subtract 1 time/date from another + get a totalminutes answer
ok thanks for replying. that looks usable.
next question: i have a variable, startTime
how would i declare that + initialize it as date + time now?
at some point in the app, i also need to increment startTime by 1 hour.
can you help with that?
thanks for the help so far
i did some research + resolved this now:
//create new date = now
startTime = new Date();
//add 1 hour to date
startTime = new Date(startTime.getTime() + 1 * 60 * 60 * 1000);
ok, so far so good.
how do i get a timestamp from a date?
RESOLVED. thanks
If your are using Joda Time you can write something like:
Code:import org.joda.time.DateTime;
public class TimeCalculationDemo {
public static void main(String[] args) {
DateTime dateTime = new DateTime();
System.out.println("dateTime = " + dateTime);
DateTime nextHour = dateTime.plusHours(1);
System.out.println("nextHour = " + nextHour);
}
}
Joda Time offers a cleaner API way to work with date and time in Java compared with the build in java.util.Date or java.util.Calender. If you are working a lot with date time manipulation you should consider using it, because it really simplify your code.
Take a look at the following tutorial: Joda-Time
I would suggest to use either JodaTime or the Calendar API.
Code:Calendar cal = Calendar.getInstance();
cal.add(Calender.HOUR_OF_DAY, 1);
i used Dates in this particular case, as they were better suited to the application.
but i also created a java TimeSpan class:
Code:package javaTimeSpans;
/**
*
* @author Paul
*/
public class TimeSpan {
private int _days;
private int _hours;
private int _minutes;
private int _seconds;
public int Days(){
return this._days;
}
public void SetDays(int d){
this._days = d;
}
public int Hours(){
return this._hours;
}
public void SetHours(int h){
this._hours = h;
}
public int Minutes(){
return this._minutes;
}
public void SetMinutes(int m){
this._minutes = m;
}
public int Seconds(){
return this._seconds;
}
public void SetSeconds(int s){
this._seconds = s;
}
public TimeSpan(int d, int h, int m, int s){
this._days = d;
this._hours = h;
this._minutes = m;
this._seconds = s;
}
public TimeSpan(int h, int m, int s){
this._days = 0;
this._hours = h;
this._minutes = m;
this._seconds = s;
}
public Boolean Equals(TimeSpan ts){
return this.Days() == ts.Days() && this.Hours() == ts.Hours() && this.Minutes() == ts.Minutes() && this.Seconds() == ts.Seconds();
}
public TimeSpan Add(TimeSpan ts){
int s = this.Seconds() + ts.Seconds();
int m = this.Minutes() + ts.Minutes();
int h = this.Hours() + ts.Hours();
int d = this.Days() + ts.Days();
if(s > 59){
s -= 60;
m += 1;
}
if(m > 59){
m -= 60;
h += 1;
}
if(h > 23){
h -= 24;
d += 1;
}
return new TimeSpan(d,h,m,s);
}
public TimeSpan Subtract(TimeSpan ts){
int s1 = this.Seconds();
int m1 = this.Minutes() * 60;
int h1 = this.Hours() * 60 * 60;
int d1 = this.Days() * 24 * 60 * 60;
int s2 = ts.Seconds();
int m2 = ts.Minutes() * 60;
int h2 = ts.Hours() * 60 * 60;
int d2 = ts.Days() * 24 * 60 * 60;
int sd = (s1+m1+h1+d1) - (s2+m2+h2+d2);
int d = sd / (int)(24 * 60 * 60);
sd -= (d * (24 * 60 * 60));
int h = sd / (int)(60 * 60);
sd -= (h * (60 * 60));
int m = sd / 60;
int s = sd - (m * 60);
return new TimeSpan(d,h,m,s);
}
public int TotalHours() {
return (this.Days() * 24) + this.Hours();
}
public int TotalMinutes() {
return (((this.Days() * 24) + this.Hours()) * 60) + this.Minutes();
}
public int TotalSeconds() {
return (((((this.Days() * 24) + this.Hours()) * 60) + this.Minutes()) * 60) + this.Seconds();
}
@Override public String toString() {
if(this.Days() != 0){
return String.format("%d:%02d:%02d:%02d", this.Days(),this.Hours(),this.Minutes(),this.Seconds());
}
else {
return String.format("%02d:%02d:%02d", this.Hours(),this.Minutes(),this.Seconds());
}
}
}
I would make the class immutable.
Hi,
to get date and initialize to now you can make Date d = new Date();
if you want to add to time then you need to create Calendar (java.util.Calendar)
Calendar cal = Calendar.getInstance(); (cal is set to now), if you need to set to different time you need to enter
cal.setTime(d); where d is Date (java.util.Date). If you want to add hours you need to do this
cal.add(Calendar.HOUR, 1); (there are also Calendar.MONTH, Calenda.MINUTE, ... etc)
if you want so subtract then make add with minus sign, cal.add(Calendar.HOUR, -1);
finally to get date from calendar just put Date calculatedDate = cal.getTime();
Code:import java.util.Calendar;
import java.util.Date;
public class test {
public static void main(String[] args) {
// creates calendar from now
Calendar cal = Calendar.getInstance();
// you can add, minutes, seconds, days, etc ..
// to subtract add with minus
cal.add(Calendar.MINUTE, 1);
Date d = cal.getTime();
}
}