• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Subtract number of days from a date

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I am trying to calculate the billing period for a client, based on BILLDATE and current day. The billing period is always 30 days (i suppose). What i want to do is, knowing the bill day(specific date), to subtract from it 30 days and to get the day when billing period actually began. It is kinda tricky, knowing that months have diferent number of days.

Is there an uncomplicated way of doing this in Java?

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Calendar class in general and the Calendar.roll method in particular.
Ignore that bit. Calendar.add (as suggested by Silviu) is better for what you want than Calendar.roll

Note that there are new Time and Date classes in java 8 so there may be an even better way of doing it, but I haven't looked at them yet so can't say for sure.
Using the Calendar class will ensure your code works with any version of Java.
 
Ranch Hand
Posts: 61
1
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Laura,

Using JDK 8, you can easily do that. Here is a useful tutorial: http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
Using JDK 7 and earlier, Calendar class can help you(or Joda Time, if you're familiar with). Assuming c is a calendar instance, c.add(Calendar.DATE, -30);

Best regards,
Silviu
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another Tutorial. Use the Java8 classes if at all possible.
 
Ranch Hand
Posts: 91
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JodaTime will make it simple and is more reliable if you are on JDK7 and below
 
A Misa
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, guy! I ended up using GregorianCalendar to set a customized date, since subtracting days is not pretty accurate in my case, for calculating a billing period.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Laura Miclescu wrote:Thanks, guy! I ended up using GregorianCalendar to set a customized date, since subtracting days is not pretty accurate in my case, for calculating a billing period.


I have to admit still being slightly mystified by your requirements. If they are indeed, as you said in your OP, "always 30 days", then you can subtract (or add) 30 days from any java.util.Date.

If, on the other hand "30 days" means:
  • A "calendar month".
  • Some period that doesn't include "weekends" or "days" actually means "working days".
  • then you'll probably need a Calendar (or, as suggested, one of the v8/joda classes.

    Winston
     
    Sheriff
    Posts: 22783
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Laura Miclescu wrote:Thanks, guy! I ended up using GregorianCalendar to set a customized date, since subtracting days is not pretty accurate in my case, for calculating a billing period.


    I have to admit still being slightly mystified by your requirements. If they are indeed, as you said in your OP, "always 30 days", then you can subtract (or add) 30 days from any java.util.Date.


    Except that the length of a day is not always 24*3600*1000 ms. Daylight savings time gives you one day per year that's 1 hour longer and one that's 1 hour shorter. That's what makes day-arithmetic tricky when not done using Calendar or java.time.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:Except that the length of a day is not always 24*3600*1000 ms. Daylight savings time gives you one day per year that's 1 hour longer and one that's 1 hour shorter. That's what makes day-arithmetic tricky when not done using Calendar or java.time.


    Good point; although in truth I suspect it's one for the lawyers.

    You could also get around it by either:
    (a) Making sure that any program that determines "past due" status doesn't run between midnight and 3am local time.
    (b) Making the billing period 30 days + 1 hour (or "DST difference").
    Personally, I like the latter.

    Winston
     
    Saloon Keeper
    Posts: 15510
    363
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:You could also get around it by either:
    [...]



    Or by simply using Joda or java.time :P
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:Or by simply using Joda or java.time :P


    But that's a philosophical discussion, surely.

    In general, I agree with you; but what is a "billing period"? It's whatever you decide it to be. And adding an hour (or even 2) to it isn't likely to "break a business".

    And then you can use a simple algorithm as opposed to a complicated one.

    It's not the "billing period" that's at fault; but the fact that we puny humans have compiicated the business of local time.

    KISS - It still works.

    Winston
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15510
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:And then you can use a simple algorithm as opposed to a complicated one.


     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:


    Fair enough.

    I guess my post is aimed that those that did without that class for 20 years.

    Winston
     
    reply
      Bookmark Topic Watch Topic
    • New Topic