Προβλημα με προσθεση ημερών σε ημερομηνία

Συζητήσεις για την Java

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
pantelispyla
Δημοσιεύσεις: 45
Εγγραφή: 06 Νοέμ 2009 10:20

Προβλημα με προσθεση ημερών σε ημερομηνία

Δημοσίευση από pantelispyla » 30 Ιούλ 2010 13:52

Έχω τον παρακάτω κώδικα:

Κώδικας: Επιλογή όλων

CallableStatement stmt7 = con.prepareCall("select * from `companyagainst`,`company`,`activities`,`income` where `companyagainst`.id='" + companyagid + "' and `companyagainst`.company_id=company.id and `companyagainst`.activities_id=activities.id and `companyagainst`.income_id=income.id");
            ResultSet rs7 = stmt7.executeQuery();
            rs7.first();

            CallableStatement stmt_dose = con.prepareCall("select * from dosepaidag where companyagainst_id='" + companyagid + "'");
            ResultSet rs_dose = stmt_dose.executeQuery();
            rs_dose.beforeFirst();

            String day = "";
            String month = "";
            String year = "";
            String potential = "";


            int when_recursive = rs7.getInt("whenrecursive");
            System.out.println(when_recursive);

            Calendar pot_when = Calendar.getInstance();
            String now = today().toString();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            Date today = sdf.parse(now);
            Date potential_date = new Date();

            if (rs_dose.next()) {
                rs_dose.last();
                System.out.println("potential");
                String base_potential = rs_dose.getString("potential");
                day = base_potential.substring(8, 10);
                month = base_potential.substring(5, 7);
                year = base_potential.substring(0, 4);
                potential = year + "-" + month + "-" + day;
                potential_date = sdf.parse(potential);
//.....month convertion
                if (month.equalsIgnoreCase("01")) {
                    month = "0";
                }
                if (month.equalsIgnoreCase("02")) {
                    month = "1";
                }
                if (month.equalsIgnoreCase("03")) {
                    month = "2";
                }
                if (month.equalsIgnoreCase("04")) {
                    month = "3";
                }
                if (month.equalsIgnoreCase("05")) {
                    month = "4";
                }
                if (month.equalsIgnoreCase("06")) {
                    month = "5";
                }
                if (month.equalsIgnoreCase("07")) {
                    month = "6";
                }
                if (month.equalsIgnoreCase("08")) {
                    month = "7";
                }
                if (month.equalsIgnoreCase("09")) {
                    month = "8";
                }
                if (month.equalsIgnoreCase("10")) {
                    month = "9";
                }
                if (month.equalsIgnoreCase("11")) {
                    month = "10";
                }
                if (month.equalsIgnoreCase("12")) {
                    month = "11";
                }

//.....day convertion
                if (day.equalsIgnoreCase("01")) {
                    day = "1";
                }
                if (day.equalsIgnoreCase("02")) {
                    day = "2";
                }
                if (day.equalsIgnoreCase("03")) {
                    day = "3";
                }
                if (day.equalsIgnoreCase("04")) {
                    day = "4";
                }
                if (day.equalsIgnoreCase("05")) {
                    day = "5";
                }
                if (day.equalsIgnoreCase("06")) {
                    day = "6";
                }
                if (day.equalsIgnoreCase("07")) {
                    day = "7";
                }
                if (day.equalsIgnoreCase("08")) {
                    day = "8";
                }
                if (day.equalsIgnoreCase("09")) {
                    day = "9";
                }

                if &#40;potential_date.compareTo&#40;today&#41; < 0&#41; &#123;
                    System.out.println&#40;"to potential einai mikrotero apo to simera ara to potential pou prepei na mpei einai to epomeno"&#41;;
                    pot_when.set&#40;Integer.parseInt&#40;year&#41;, Integer.parseInt&#40;month&#41;, Integer.parseInt&#40;day&#41;&#41;;
                    pot_when.add&#40;Calendar.DATE, when_recursive&#41;;
                    year = Integer.toString&#40;pot_when.get&#40;Calendar.YEAR&#41;&#41;;
                    month = Integer.toString&#40;pot_when.get&#40;Calendar.MONTH&#41;&#41;;
                    day = Integer.toString&#40;pot_when.get&#40;Calendar.DATE&#41;&#41;;

                    //.....month convertion
                    if &#40;month.equalsIgnoreCase&#40;"0"&#41;&#41; &#123;
                        month = "01";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"1"&#41;&#41; &#123;
                        month = "02";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"2"&#41;&#41; &#123;
                        month = "03";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"3"&#41;&#41; &#123;
                        month = "04";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"4"&#41;&#41; &#123;
                        month = "05";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"5"&#41;&#41; &#123;
                        month = "06";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"6"&#41;&#41; &#123;
                        month = "07";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"7"&#41;&#41; &#123;
                        month = "08";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"8"&#41;&#41; &#123;
                        month = "09";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"9"&#41;&#41; &#123;
                        month = "10";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"10"&#41;&#41; &#123;
                        month = "11";
                    &#125;
                    if &#40;month.equalsIgnoreCase&#40;"11"&#41;&#41; &#123;
                        month = "12";
                    &#125;

                    //.....day convertion
                    if &#40;day.equalsIgnoreCase&#40;"1"&#41;&#41; &#123;
                        day = "01";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"2"&#41;&#41; &#123;
                        day = "02";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"3"&#41;&#41; &#123;
                        day = "03";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"4"&#41;&#41; &#123;
                        day = "04";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"5"&#41;&#41; &#123;
                        day = "05";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"6"&#41;&#41; &#123;
                        day = "06";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"7"&#41;&#41; &#123;
                        day = "07";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"8"&#41;&#41; &#123;
                        day = "08";
                    &#125;
                    if &#40;day.equalsIgnoreCase&#40;"9"&#41;&#41; &#123;
                        day = "09";
                    &#125;

                    potential = year + "-" + month + "-" + day;
                &#125;
                System.out.println&#40;"------today = " + today + "-------- potential = " + potential&#41;;
                System.out.println&#40;"yparxei dosi pliromeni"&#41;;

            &#125; else &#123;
                String date_start = rs7.getString&#40;"date_start"&#41;;
                day = date_start.substring&#40;8, 10&#41;;
                month = date_start.substring&#40;5, 7&#41;;
                year = date_start.substring&#40;0, 4&#41;;

                //.....month convertion
                if &#40;month.equalsIgnoreCase&#40;"01"&#41;&#41; &#123;
                    month = "0";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"02"&#41;&#41; &#123;
                    month = "1";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"03"&#41;&#41; &#123;
                    month = "2";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"04"&#41;&#41; &#123;
                    month = "3";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"05"&#41;&#41; &#123;
                    month = "4";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"06"&#41;&#41; &#123;
                    month = "5";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"07"&#41;&#41; &#123;
                    month = "6";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"08"&#41;&#41; &#123;
                    month = "7";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"09"&#41;&#41; &#123;
                    month = "8";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"10"&#41;&#41; &#123;
                    month = "9";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"11"&#41;&#41; &#123;
                    month = "10";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"12"&#41;&#41; &#123;
                    month = "11";
                &#125;

//.....day convertion
                if &#40;day.equalsIgnoreCase&#40;"01"&#41;&#41; &#123;
                    day = "1";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"02"&#41;&#41; &#123;
                    day = "2";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"03"&#41;&#41; &#123;
                    day = "3";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"04"&#41;&#41; &#123;
                    day = "4";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"05"&#41;&#41; &#123;
                    day = "5";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"06"&#41;&#41; &#123;
                    day = "6";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"07"&#41;&#41; &#123;
                    day = "7";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"08"&#41;&#41; &#123;
                    day = "8";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"09"&#41;&#41; &#123;
                    day = "9";
                &#125;

                pot_when.set&#40;Integer.parseInt&#40;year&#41;, Integer.parseInt&#40;month&#41;, Integer.parseInt&#40;day&#41;&#41;;
                pot_when.add&#40;Calendar.DATE, when_recursive&#41;;
                year = Integer.toString&#40;pot_when.get&#40;Calendar.YEAR&#41;&#41;;
                month = Integer.toString&#40;pot_when.get&#40;Calendar.MONTH&#41;&#41;;
                day = Integer.toString&#40;pot_when.get&#40;Calendar.DATE&#41;&#41;;

                //.....month convertion
                if &#40;month.equalsIgnoreCase&#40;"0"&#41;&#41; &#123;
                    month = "01";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"1"&#41;&#41; &#123;
                    month = "02";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"2"&#41;&#41; &#123;
                    month = "03";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"3"&#41;&#41; &#123;
                    month = "04";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"4"&#41;&#41; &#123;
                    month = "05";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"5"&#41;&#41; &#123;
                    month = "06";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"6"&#41;&#41; &#123;
                    month = "07";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"7"&#41;&#41; &#123;
                    month = "08";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"8"&#41;&#41; &#123;
                    month = "09";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"9"&#41;&#41; &#123;
                    month = "10";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"10"&#41;&#41; &#123;
                    month = "11";
                &#125;
                if &#40;month.equalsIgnoreCase&#40;"11"&#41;&#41; &#123;
                    month = "12";
                &#125;

                //.....day convertion
                if &#40;day.equalsIgnoreCase&#40;"1"&#41;&#41; &#123;
                    day = "01";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"2"&#41;&#41; &#123;
                    day = "02";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"3"&#41;&#41; &#123;
                    day = "03";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"4"&#41;&#41; &#123;
                    day = "04";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"5"&#41;&#41; &#123;
                    day = "05";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"6"&#41;&#41; &#123;
                    day = "06";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"7"&#41;&#41; &#123;
                    day = "07";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"8"&#41;&#41; &#123;
                    day = "08";
                &#125;
                if &#40;day.equalsIgnoreCase&#40;"9"&#41;&#41; &#123;
                    day = "09";
                &#125;

                potential = year + "-" + month + "-" + day;
                System.out.println&#40;"------today = " + today + "-------- potential = " + potential&#41;;
                System.out.println&#40;"den yparxei dosi pliromeni"&#41;;
            &#125;
Ο κώδικας προσθέτει κάποιες μέρες σε μια ημερομηνία. Το πόσες μέρες προσθέτει το πέρνει από τη βάση. Προσθέτω κανονικά ημέρες αλλά όταν όταν ειναι να προσθέσει και να μπει στον Οκτώβριο δεν το κάνει και πετάγεται στο Δεκέμβριο. Σαν να πηδάει 2 μήνες. Γιατί το κάνει αυτό; Δεν νομίζω να κάνω κάτι λάθος.

Άβαταρ μέλους
virxen75
Δημοσιεύσεις: 493
Εγγραφή: 18 Φεβ 2009 00:17
Τοποθεσία: ΗΡΑΚΛΕΙΟ ΚΡΗΤΗΣ

Προβλημα με προσθεση ημερών σε ημερομηνία

Δημοσίευση από virxen75 » 31 Ιούλ 2010 02:21

δες αυτό μήπως σε βοηθήσει

Κώδικας: Επιλογή όλων

import java.util.*;
public class DateAdd&#123;
	public DateAdd&#40;&#41;&#123;
		System.out.println&#40;"date=29/12/2008"&#41;;
		System.out.println&#40;"days to add=4!!"&#41;;
		System.out.println&#40;"results!!!"&#41;;
		System.out.println&#40;dateAddDays&#40;"29","12","2008",4&#41;&#41;;
	&#125;
	public String dateAddDays&#40;String day,String month,String year,int days&#41;&#123;
		Calendar calendar=Calendar.getInstance&#40;&#41;;
		String result="";
		calendar.set&#40;Integer.parseInt&#40;year&#41;,Integer.parseInt&#40;month&#41;,Integer.parseInt&#40;day&#41;&#41;;
		calendar.add&#40;Calendar.DATE,days&#41;;
		result=calendar.get&#40;Calendar.DATE&#41;+"-"+calendar.get&#40;Calendar.MONTH&#41;+"-"+calendar.get&#40;Calendar.YEAR&#41;;
		return result;
	&#125;
	public static void main&#40;String args&#91;&#93;&#41;&#123;
		new DateAdd&#40;&#41;;
	&#125;
&#125;

Απάντηση

Επιστροφή στο “Java”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες