Results 1 to 13 of 13
  1. #1
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Question java (7) equivalent of .Net timespan

    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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default Re: java (7) equivalent of .Net timespan

    Quote Originally Posted by .paul. View Post
    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
    Given a Date object you can get its time stamp; it's the number of milli seconds since epoch (tipically the first of January 1970); when you subtract two time stamps you get the difference in time measured in milli seconds.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    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

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: java (7) equivalent of .Net timespan

    Quote Originally Posted by .paul. View Post
    i have a variable, startTime

    how would i declare that + initialize it as date + time now?
    What type of variable is startTime?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    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);

  6. #6
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    ok, so far so good.

    how do i get a timestamp from a date?

    RESOLVED. thanks
    Last edited by .paul.; 07-27-2012 at 12:30 AM.

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: java (7) equivalent of .Net timespan

    Quote Originally Posted by .paul. View Post
    at some point in the app, i also need to increment startTime by 1 hour.
    can you help with that?
    If your are using Joda Time you can write something like:

    Java 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);
        }
    }

  8. #8
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    Quote Originally Posted by wsaryada View Post
    If your are using Joda Time you can write something like:

    Java 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);
        }
    }
    i've noticed joda.time mentioned on the internet, but i'm a beginner with java + i'm guessing it's a 3rd party class, or an obscure feature of java that i haven't found yet.

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: java (7) equivalent of .Net timespan

    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

  10. #10
    noctarius is offline Member
    Join Date
    Jun 2012
    Location
    Remscheid, Germany
    Posts
    61
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    I would suggest to use either JodaTime or the Calendar API.

    Java Code:
    Calendar cal = Calendar.getInstance();
    cal.add(Calender.HOUR_OF_DAY, 1);

  11. #11
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    i used Dates in this particular case, as they were better suited to the application.

    but i also created a java TimeSpan class:

    Java 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());
            }
        }
        
    }

  12. #12
    noctarius is offline Member
    Join Date
    Jun 2012
    Location
    Remscheid, Germany
    Posts
    61
    Rep Power
    0

    Default Re: java (7) equivalent of .Net timespan

    I would make the class immutable.

  13. #13
    kire9dk is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Post Re: java (7) equivalent of .Net timespan

    Quote Originally Posted by .paul. View Post
    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
    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();

    Java 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();
    	}
    }
    Last edited by kire9dk; 07-29-2012 at 04:40 PM.

Similar Threads

  1. Replies: 1
    Last Post: 07-14-2011, 01:14 AM
  2. C IO port function equivalent in Java
    By Z.K. in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 10:13 PM
  3. Equivalent of PHP isset in JAVA
    By africanhacker in forum New To Java
    Replies: 11
    Last Post: 03-24-2011, 02:41 PM
  4. Java equivalent of PHP's variable-variables?
    By XmisterIS in forum New To Java
    Replies: 7
    Last Post: 09-01-2010, 11:05 AM
  5. Php explode function, equivalent java
    By fernando in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •