Java - mostrar la fecha mas antigua

 
Vista:
sin imagen de perfil

mostrar la fecha mas antigua

Publicado por Horacio (1 intervención) el 11/04/2016 23:28:53
hola amigos estoy desarrollando un sistema de cobranza donde quiero que me muestre la deuda mas antigua de una cadena que ingrese realice la siguiente funcion pero me devuelven varias deudas y yo solo quiero que me devuelva una deuda la mas antigua paso el codigo y el resultado de dicho codigo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public static void main(String[] args) throws Exception{
	String cad="62640|4610381|333873|1|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-03-09|10075224|1|90.00 USD*FIN*62640|4610381|333873|2|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-04-09|10075224|1|90.00 USD*FIN*62640|4610381|333873|3|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-05-09|10075224|1|90.00 USD*FIN*62640|4610381|333873|4|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-06-09|10075224|1|90.00 USD*FIN*";
	String[] array = cad.split("FIN");
	String servicio_fecha_menores="";
	int sw=0;
	int i,j;
 
	for( i=0;i<array.length;i++){
		sw=0;
		System.out.println("============= vuelta: "+i+" ===========");
		String linea = array[i].trim();
		System.out.println("-> " + linea );
		for (j = i+1; j < array.length-1; j++) {
			System.out.println("comparando "+ getValor(array[i], 5)+" <-> "+getValor(array[j], 5));
			if(getValor(array[i], 5).equalsIgnoreCase(getValor(array[j], 5))){
				sw=1;
				System.out.println("guardar la fecha menor por q son iguales: ->");
				//System.out.println(getValor(array[i],2));
 
				if(getValor(array[i], 7).compareTo(getValor(array[j], 7)) < 0){
					//  System.out.println(array[i]);
					servicio_fecha_menores=servicio_fecha_menores+array[i]+"FIN";
				}else
					if(getValor(array[j], 7).compareTo(getValor(array[i], 7)) < 0){
						servicio_fecha_menores=servicio_fecha_menores+array[j]+"FIN";
				}
				//else servicio_fecha_menores=servicio_fecha_menores+array[i]+"!";
			}
 
		}
		if(sw==0){
			String palabra=getValor(array[i], 5);
			if(servicio_fecha_menores.indexOf(palabra)>-1){
				System.out.println("ya existe guardado esta palabra");
			}
			else {System.out.println("guardar por q es el unico");
				servicio_fecha_menores=servicio_fecha_menores+array[i]+"FIN";
			}
		}
 
	}
	servicio_fecha_menores+="*";
	System.out.println("cadena -> "+servicio_fecha_menores);
 
}
 
public static String getValor(String cadena, int posicion) throws Exception {
	int contador = 0;
	int posicionActual = 0;
	String valor = "";
 
	do {
		posicionActual = cadena.indexOf(("|"));
		if (posicionActual == -1) {
			valor = "";
			contador = posicion;
		} else {
			valor = cadena.substring(0, posicionActual);
			cadena = cadena.substring((posicionActual+1), cadena.length());
			contador++;
		}
	} while(contador < posicion);
 
	return valor;
}


RESULTADO


============= vuelta: 0 ===========
-> 62640|4610381|333873|1|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-03-09|10075224|1|90.00 USD*
comparando AUTOMOTORES <-> AUTOMOTORES
guardar la fecha menor por q son iguales: ->
comparando AUTOMOTORES <-> AUTOMOTORES
guardar la fecha menor por q son iguales: ->
comparando AUTOMOTORES <-> AUTOMOTORES
guardar la fecha menor por q son iguales: ->
============= vuelta: 1 ===========
-> *62640|4610381|333873|2|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-04-09|10075224|1|90.00 USD*
comparando AUTOMOTORES <-> AUTOMOTORES
guardar la fecha menor por q son iguales: ->
comparando AUTOMOTORES <-> AUTOMOTORES
guardar la fecha menor por q son iguales: ->
============= vuelta: 2 ===========
-> *62640|4610381|333873|3|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-05-09|10075224|1|90.00 USD*
comparando AUTOMOTORES <-> AUTOMOTORES
guardar la fecha menor por q son iguales: ->
============= vuelta: 3 ===========
-> *62640|4610381|333873|4|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-06-09|10075224|1|90.00 USD*
ya existe guardado esta palabra
============= vuelta: 4 ===========
-> *
ya existe guardado esta palabra
cadena RESULATADO FINAL-> 62640|4610381|333873|1|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-03-09|10075224|1|90.00 USD*FIN62640|4610381|333873|1|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-03-09|10075224|1|90.00 USD*FIN62640|4610381|333873|1|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-03-09|10075224|1|90.00 USD*FIN*62640|4610381|333873|2|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-04-09|10075224|1|90.00 USD*FIN*62640|4610381|333873|2|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-04-09|10075224|1|90.00 USD*FIN*62640|4610381|333873|3|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-05-09|10075224|1|90.00 USD*FIN*


DONDE SOLO DEBERIA ALMACENARME LA SIGUIENTE CADENA QUE ES LA MENOR DE TODAS COMO RESULTADO FINAL
62640|4610381|333873|1|AUTOMOTORES|BRAVO ANGLARILL, ROY LUIS|2016-03-09|10075224|1|90.00 USD*

AYUDA PORFAVOR
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder