Solved

how to do a countdown timer for 3 months

Posted on 2012-03-30
16
533 Views
Last Modified: 2012-05-08
please help i want a count down timer code for months.
count = 3
check current month = 03
decrement count by 1
and after three months ie month=06
count = 0.
0
Comment
Question by:Newton21205
  • 13
  • 3
16 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
please, elaborate what do you mean by that question ?
Do you have some code ?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
perhpas something like thios:

java.util.Date d30 = new java.util.Date();
SimpleDateFormat sdf30  = new SimpleDateFormat("MM");
String month = sdf30.format(d30);
int intMonth = Integer.parseInt(month);

while(int j=0; j<3; j++) {
 intMonth++;

// do somthing with it 

}
 

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
If you want to do it in real time - better use windows scheduler and run sime task every month - write into file the month number
0
 

Author Comment

by:Newton21205
Comment Utility
i mean to say that the current month is March so on  June that is after three months the counter should be zero.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
String [] monthStrings = {"Jan","Feb",Mar","Apr","May","Jun","Jul","Aug","sep","Oct","Nov","Dec"};

java.util.Date d30 = new java.util.Date();
SimpleDateFormat sdf30  = new SimpleDateFormat("MM");
String month = sdf30.format(d30);
int intMonth = Integer.parseInt(month);

int count = intMonth;

while((count >0) {
 intMonth++;
count--;
System.out.println(monthsStrings[intMonth-1]);




}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
This code I tested and it's working:

  String [] monthStrings = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","sep","Oct","Nov","Dec"};

java.util.Date d30 = new java.util.Date();
SimpleDateFormat sdf30  = new SimpleDateFormat("MM");
String month = sdf30.format(d30);
int intMonth = Integer.parseInt(month);

int count = intMonth;

    String monthName = "";    

while(count >0) {
 intMonth++;
count--;
 monthName =    monthStrings[intMonth-1];





}
        System.out.println(monthName);

Open in new window


Output:

Jun

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
This will do the same without having the array of months names:

java.util.Date d30 = new java.util.Date();
SimpleDateFormat sdf30  = new SimpleDateFormat("MM");
String month = sdf30.format(d30);
int intMonth = Integer.parseInt(month);

int count = intMonth;


while(count >0) {
 intMonth++;
count--;





}
        java.util.Date d301 = sdf30.parse(""+intMonth, new ParsePosition(0));


        SimpleDateFormat sdf301  = new SimpleDateFormat("MMM");

               System.out.println(sdf301.format(d301));

Open in new window


Output:

Jun

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
and if you use this format:

  SimpleDateFormat sdf301  = new SimpleDateFormat("MMMMMM");

it will print the full name of the month , insted of first three chars
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Newton21205
Comment Utility
for_yan: i want it for real time.
this is what im doin:
        Calendar notificationInterval = new GregorianCalendar();
        Date currentPaymentDate = subscription.getStartDate();
// set the timer here so after three months it will notify
        Date nextExpectedPaymentDate = currentPaymentDate + 3 months
0
 

Author Comment

by:Newton21205
Comment Utility
currentPaymentDate  is in format dd/MM/yyyy
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
If you want for real life - then you can use Java Quartz schedluer or TimerTask - however I would not recommend it
as you would need to maintain that java job for all the time to get you once a month notification
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
I think yiu woull be better off if you use the Windows scheduler to schedule the program running notingfucayion at requirted date
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Jus write the program which will sedn notification, make a batch dfile out of it and go to winodws scheduler and schedule it to run say every three months or at any other schedule which you prefer
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
To schedule in Winods is simple - go to Control panel - Task scheduler - click on add scheduled task , browsee to your batch file, then select Monthly schedule - and select those months you want your job to run

TYou'll not need to mainatin special java process running all the time on your machine to count months and you don't want additiional service, etc
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
That is how you add months with calendar:
    Calendar cal = Calendar.getInstance();

        
        cal.add(Calendar.MONTH, 3);
        

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
           SimpleDateFormat sdf303 = new SimpleDateFormat("dd/MM/yyyy");

              

        Calendar cal = Calendar.getInstance();


        cal.add(Calendar.MONTH, 3);
        
           System.out.println(sdf303.format(cal.getTime()));

Open in new window


Output:

30/06/2012

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json example 39 110
Exception after setting jdbc session management 2 38
java set up 1 45
wordmultiple challenge 12 89
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now