Advertisement
urksiful

Order dates by date and time

Aug 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1.  
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6.  
  7.  
  8. /**
  9.  *
  10.  * @author Uriel
  11.  */
  12. public class Comparar {
  13.  
  14.     public static void main(String[] args) {
  15.         ArrayList<vo_order> lista = new ArrayList<>();
  16.  
  17.         lista.add(new vo_order("Dorilocos", "12:34:21", "07-06-2018"));
  18.         lista.add(new vo_order("Pizza Italiana", "12:35:32", "07-06-2018"));
  19.         lista.add(new vo_order("Albóndigas de Res", "19:22:45", "07-06-2018"));
  20.         lista.add(new vo_order("Jugo de Naranja", "12:34:49", "07-06-2018"));
  21.         lista.add(new vo_order("Mackis de Salmón", "12:34:48", "07-06-2018"));
  22.         lista.add(new vo_order("Taco de Pastor", "09:34:21", "06-06-2018"));
  23.         lista.add(new vo_order("Arrachera de Res Granitada", "12:34:21", "07-06-2018"));
  24.         lista.add(new vo_order("Enchiladas Suizas", "11:32:41", "07-06-2018"));
  25.        
  26.         for (vo_order object : lista) {
  27.             System.out.println(object.toText());
  28.         }
  29.         System.out.println("===============================");
  30.         orderingOrders(lista);
  31.          for (vo_order object : lista) {
  32.             System.out.println(object.toText());
  33.         }
  34.        
  35.     }
  36.  
  37.     public static void orderingOrders(ArrayList<vo_order> pedidos) {
  38.        
  39.         vo_order aux;
  40.         for (int i = 0; i < pedidos.size() - 1; i++) {
  41.             for (int j = 0; j < pedidos.size() - i - 1; j++) {
  42.                 if (lessTime(pedidos.get(j).getFecha_Registro_P(), pedidos.get(j).getTiempo_P(), pedidos.get(j+1).getFecha_Registro_P(), pedidos.get(j+1).getTiempo_P())) {
  43.                      aux = pedidos.get(j);
  44.                      pedidos.set(j, pedidos.get(j+1));
  45.                      pedidos.set(j+1, aux);
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.     private static boolean lessTime(String dd1, String dt1, String dd2, String dt2) {
  52.         boolean result = false;
  53.         try {            
  54.             SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  55.             Date fechaDate1 = formateador.parse(dd1+" "+dt1);
  56.             Date fechaDate2 = formateador.parse(dd2+" "+dt2);
  57.            
  58.  
  59.             if (fechaDate1.before(fechaDate2)) {
  60.                 result = true;
  61.            }
  62.         } catch (ParseException e) {;}
  63.         return result;
  64.     }
  65.    
  66.    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement