Compute any day in calendar using Java.Util package
Computing a day on any date is not an easy task,it can't be done so easily.By using below given source code you can compute any day which will come after your mentioned date whether it may be tomorrow or after 25 years very easily.Java.Util package has been used.This code is developed when I was doing coding for railway reservation system and the condition was to make reservation in advance say 30 days or more.This Program takes input in the specified date format which is mentioned in static method now().This method returns a String of current date.Here we have taken the two dates specifically but you can add interactivity in your program by taking date as a input.But note that the dates must be of same format.
Example(Source Code):-
// Created by vaibhav Pandey
//diffdays.java
import java.util.Calendar;
import java.io.Serializable;
import java.util.Date;
import java.text.SimpleDateFormat;
public class diffdays
{
public static String now(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
public static void main(String a[])
{
long diffDays;
int k;
String tempTo="";
String tempFrom="";
SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date from =new Date(diffdays.now("yyyy/MM/dd"));
Date to =new Date("2009/05/10");
long diffMillis = to.getTime() - from.getTime();
diffDays = diffMillis / (1000 * 60 * 60 * 24);
System.out.println("Format Used is yyyy/mm/dd");
System.out.println(diffdays.now("yyyy/MM/dd"));
long i=0;
String today=(diffdays.now("EEEEEE"));
String day_computed="";
System.out.println("Today is:- "+today);
if(today.compareTo("Sunday")==0)
{
i=1;
}
else if(today.compareTo("Monday")==0)
{
i=2;
}
if(today.compareTo("Tuesday")==0)
{
i=3;
}
if(today.compareTo("Wednesday")==0)
{
i=4;
}
if(today.compareTo("Thursday")==0)
{
i=5;
}
if(today.compareTo("Friday")==0)
{
i=6;
}
if(today.compareTo("Saturday")==0)
{
i=7;
}
i=(i+(diffDays-1)%7);
int j=(int)((i%7));
switch(j)
{
case 0:day_computed="Sunday";
break;
case 1:day_computed="Monday";
break;
case 2:day_computed="Tueday";
break;
case 3:day_computed="Wednesday";
break;
case 4:day_computed="Thursday";
break;
case 5:day_computed="Friday";
break;
case 6:day_computed="Saturday";
break;
}
System.out.println("Day on 2009/05/10 is "+day_computed);
System.out.println("Created by vaibhav pandey on 2009/05/06");
}
}
Output:-In the output you will get following screen showing following values:-
H:\>java diffdays
>Format used is yyyy/mm/dd
>2009/05/06
>Today is:- Thursday
>Day on 2009/05/10 is Sunday
>Created by Vaibhav Pandey on 2009/05/06
Illustration:-As in previous post where I told you how can you get date your specified format,we will use it here also coz it is needed to get date in desired format to save complexity and time.The logic of this is implemented in now() method.Coming to main() method you can see we have taken two variables of date type and passed same type of date format to them.Then we have computed the difference in milliseconds between both dates.According to which we have calculated difference in days by dividing the value by (1000*60*60*24).
Coming to the implementation logic ,here we computed current day and matched it to get the value of ' i ' in switch() construct.After getting this value we applied the formula (i+(diffdays-1)%7) to get the resulting value of ' i ' which will take us the resultant day.This formula computes the value of ' i ' by adding remiander of (diffdays-1)%7.Actually only (diffdays%7) would have worked but since I have taken it so I have to work accordingly.Lets make it simple assume that today is monday the from first switch we will get i=2,and we wish to compute day after 4 days, thus diffdays would be equal to 4.Now applying the formula we get
i=(2+(4-1)%7)
=> i = (2+3) //since 3%7=3
=> i = 5.
going to next switch() and getting day according to value of j we get day=" Friday" which is desired result.We used ' j ' here to use in switch() because ' i ' being long cannot be used in it.If you liked it please comment.I need your feedback desperately.
Links:-
Home
Date Time in your Specified Format
Earn Money Quick
Example(Source Code):-
// Created by vaibhav Pandey
//diffdays.java
import java.util.Calendar;
import java.io.Serializable;
import java.util.Date;
import java.text.SimpleDateFormat;
public class diffdays
{
public static String now(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
public static void main(String a[])
{
long diffDays;
int k;
String tempTo="";
String tempFrom="";
SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date from =new Date(diffdays.now("yyyy/MM/dd"));
Date to =new Date("2009/05/10");
long diffMillis = to.getTime() - from.getTime();
diffDays = diffMillis / (1000 * 60 * 60 * 24);
System.out.println("Format Used is yyyy/mm/dd");
System.out.println(diffdays.now("yyyy/MM/dd"));
long i=0;
String today=(diffdays.now("EEEEEE"));
String day_computed="";
System.out.println("Today is:- "+today);
if(today.compareTo("Sunday")==0)
{
i=1;
}
else if(today.compareTo("Monday")==0)
{
i=2;
}
if(today.compareTo("Tuesday")==0)
{
i=3;
}
if(today.compareTo("Wednesday")==0)
{
i=4;
}
if(today.compareTo("Thursday")==0)
{
i=5;
}
if(today.compareTo("Friday")==0)
{
i=6;
}
if(today.compareTo("Saturday")==0)
{
i=7;
}
i=(i+(diffDays-1)%7);
int j=(int)((i%7));
switch(j)
{
case 0:day_computed="Sunday";
break;
case 1:day_computed="Monday";
break;
case 2:day_computed="Tueday";
break;
case 3:day_computed="Wednesday";
break;
case 4:day_computed="Thursday";
break;
case 5:day_computed="Friday";
break;
case 6:day_computed="Saturday";
break;
}
System.out.println("Day on 2009/05/10 is "+day_computed);
System.out.println("Created by vaibhav pandey on 2009/05/06");
}
}
Output:-In the output you will get following screen showing following values:-
H:\>java diffdays
>Format used is yyyy/mm/dd
>2009/05/06
>Today is:- Thursday
>Day on 2009/05/10 is Sunday
>Created by Vaibhav Pandey on 2009/05/06
Illustration:-As in previous post where I told you how can you get date your specified format,we will use it here also coz it is needed to get date in desired format to save complexity and time.The logic of this is implemented in now() method.Coming to main() method you can see we have taken two variables of date type and passed same type of date format to them.Then we have computed the difference in milliseconds between both dates.According to which we have calculated difference in days by dividing the value by (1000*60*60*24).
Coming to the implementation logic ,here we computed current day and matched it to get the value of ' i ' in switch() construct.After getting this value we applied the formula (i+(diffdays-1)%7) to get the resulting value of ' i ' which will take us the resultant day.This formula computes the value of ' i ' by adding remiander of (diffdays-1)%7.Actually only (diffdays%7) would have worked but since I have taken it so I have to work accordingly.Lets make it simple assume that today is monday the from first switch we will get i=2,and we wish to compute day after 4 days, thus diffdays would be equal to 4.Now applying the formula we get
i=(2+(4-1)%7)
=> i = (2+3) //since 3%7=3
=> i = 5.
going to next switch() and getting day according to value of j we get day=" Friday" which is desired result.We used ' j ' here to use in switch() because ' i ' being long cannot be used in it.If you liked it please comment.I need your feedback desperately.
Links:-
Home
Date Time in your Specified Format
Earn Money Quick


















0 comments:
Post a Comment
Post a Comment