Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package pt.ipleiria.estg.dei.es.veiculos.modelo;
  7.  
  8. import java.util.Calendar;
  9. import java.util.GregorianCalendar;
  10.  
  11. public class Data {
  12.  
  13. private Calendar calendar;
  14.  
  15. public Data(int dia, int mes, int ano) {
  16.  
  17. calendar = new GregorianCalendar (ano,mes-1,dia);
  18.  
  19. }
  20.  
  21. public static Data parse (String data){
  22. String[] cortado = data.split("-");
  23.  
  24. int dia = Integer.parseInt( cortado [0] );
  25. int mes = Integer.parseInt( cortado [1] );
  26. int ano = Integer.parseInt( cortado [2] );
  27.  
  28.  
  29.  
  30. if (dia > 31 || dia < 1){
  31. return null;
  32. }
  33.  
  34. if (mes > 12 || mes < 1){
  35. return null;
  36. }
  37.  
  38. if (ano > Calendar.getInstance().get(Calendar.YEAR) || ano < 1970){
  39. return null;
  40. }
  41.  
  42.  
  43. return new Data (dia, mes, ano);
  44.  
  45. }
  46.  
  47. @Override
  48. public String toString() {
  49. int mes = calendar.get(calendar.MONTH)+1;
  50.  
  51. return calendar.get(calendar.DATE) + "-" + mes + "-" + calendar.get(calendar.YEAR);
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement