Wednesday, January 12, 2011

Find the previous dates based on hours and minutes.

Simple program solution


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Naveen
*/
import java.util.Calendar;
import java.util.Scanner;

public class DateFind {

 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
   // TODO code application logic here
   Scanner input = new Scanner(System.in);
   int shours = input.nextInt();
   int sminutes = input.nextInt();
   Calendar obj = Calendar.getInstance();
   int days = 0, hours = 0;
   //finds the subtracted days,hours
   if (shours >= 24) {
     days = shours / 24;
    hours = shours % 24;
   }
   for (int i = 0; i < days; i++) {
     if (obj.get(Calendar.DAY_OF_YEAR) == 1) {
       obj.roll(Calendar.YEAR, false);
     }
     obj.roll(Calendar.DAY_OF_YEAR, false);
   }
   for (int i = 0; i < hours; i++) {
     obj.roll(Calendar.HOUR, false);
   }
   for (int i = 0; i < sminutes; i++) {
     obj.roll(Calendar.MINUTE, false);
   }
   System.out.println(obj.get(Calendar.YEAR));
   System.out.println(obj.get(Calendar.MONTH));
   System.out.println(obj.get(Calendar.DAY_OF_MONTH));
   System.out.println(obj.get(Calendar.HOUR));
   System.out.println(obj.get(Calendar.MINUTE));
 }
}











below program is the very odd coded program.


/**
*
* @author Madarapu Naveen
*/

import java.util.Scanner;

class DateFind {

  public static void main(String args[]) {
   Scanner input = new Scanner(System.in);
   System.out.println("enter subtracted time in hours and minutes");
   int sh = input.nextInt();
   int smin = input.nextInt();
   System.out
.println("enter present time year enter month enter day enter hour enter minutes");
   int py = input.nextInt();
   int pm = input.nextInt();
   int pd = input.nextInt();
   int ph = input.nextInt();
   int pmin = input.nextInt();
   timeCalculation(sh, smin, py, pm, pd, ph, pmin);

  }

  public static void timeCalculation(int hour, int min, int py, int pm,
int pd, int ph, int pmin) {
   int year;
   int days;
   if (hour > 24) {
   days = hour / 24;
   hour = hour % 24;

   if (days >= 365) {
   if (py % 4 == 0) {
   year = days / 366;
   days = days % 366;
   } else {
   year = days / 365;
   days = days % 365;
   }
   } else {
   year = 0;
   }
   } else {
   year = 0;
   days = 0;
   }

   findResult(year, days, hour, min, py, pm, pd, ph, pmin);
  }

  public static void findResult(int cyear, int cdays, int chour, int cmin,
int py, int pmonth, int pd, int ph, int pmin) {

   while (cdays > 0) {

   switch (pmonth) {

   case 1:
   case 2:
   case 4:
   case 6:
   case 8:
   case 9:
   case 11:
   cdays = pd - cdays;
   if (cdays <= 0) {
   pmonth = 12;
   cdays = -cdays;
   pd = 31;
   } else {
   cdays = -cdays;
   pd = -cdays;
   }
   break;
   case 3:
   cdays = pd - cdays;
   if (cdays <= 0) {
   pmonth = 2;
   cdays = -cdays;
   pd = (cyear % 4 == 0) ? 29 : 28;
   } else {
   cdays = -cdays;
   pd = -cdays;
   }
   break;
   case 5:
   case 7:
   case 10:
   case 12:
   cdays = pd - cdays;
   if (cdays <= 0) {
   pmonth = 4;
   cdays = -cdays;
   pd = 30;
   } else {
   cdays = -cdays;
   pd = -cdays;
   }
   break;
   }
   }
// here consider as 24 hours clock
  int hour = ph - chour;
  if (hour < 0) {
  hour = 24 + hour;
  pd--;
  }
  int minutes = pmin - cmin;
  if (minutes < 0) {
  minutes = 60 + minutes;
  hour--;
  }

  System.out.println("year is " + (py - cyear));
  System.out.println("month is " + pmonth);
  System.out.println("day is " + pd);
  System.out.println("hour is " + hour);
  System.out.println("minutes is" + minutes);
  }

 }

No comments:

Post a Comment