JSR-310 Date and Time aprobado

Publicado el 16 de February de 2007 por Paco

Lo veo en javaHispano y en theServerSide. Parece que por fin se incluirá en la JDK una manera correcta y a la vez cómoda de trabajar con fechas.

La clase GregorianCalendar es extremadamente potente, pero inicializar una instancia correctamente, puede necesitar hasta unas 10 líneas de código. Ni que decir tiene que una aplicación (como la última en la que estoy trabajando) que haga un uso intensivo de fechas y horas así como incrementos y comparaciones sobre los mismos, se puede volver muy pesada.

El hecho de que se vaya o no a incluir una nueva feature en la próxima (o ya veremos cuál) JDK no me preocupa demasiado. Lo que sí que me ha gustado y mucho es el enlace a JodaTime. Una API para trabajar con fechas y horas en Java que tiene cosas tan chulas como esta:

public boolean isAfterPayDay(DateTime datetime) {
  if (datetime.getMonthOfYear() == 2) {
    return datetime.getDayOfMonth() > 26;
  }
  return datetime.getDayOfMonth() > 28;
}

public Days daysToNewYear(LocalDate fromDate) {
  LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1);
  return Days.daysBetween(fromDate, newYear);
}

public boolean isRentalOverdue(DateTime datetimeRented) {
  Period rentalPeriod = new Period().withDays(2).withHours(12);
  return datetimeRented.plus(rentalPeriod).isBeforeNow();
}

public String getBirthMonthText(LocalDate dateOfBirth) {
  return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH);
}

Si tenéis que programar algo que haga uso de fechas y horas más allá del new Timestamp() os recomiendo que no os perdáis JodaTime.

Por cierto, inauguré hace poco la sección Java. Llevo como 8 años trabajando en Java y JEE y hasta ahora no había escrito prácticamente nada sobre java principalmente por su licencia. Ahora, GPL, me ha animado a publicar alguna que otra cosilla :-)

Si tenéis alguna curiosidad al respecto u os interesa un artículo sobre algún tema pedid y me lo planteo.

Publicado bajo Java, Software |

2 comentarios

  1. Gravatar del autor del comentario

    JodaTime és la resposata al malson de treballar amb dates amb Java. No sé exactament com acabara la cosa, però si per mi fos votaria per que agafessin el codi del JodaTime i directament ho posessin com a implementació oficial.

  2. Gravatar del autor del comentario

    Segur que s’hi assembla molt :-)


Deja un comentario

Por favor, ten en cuenta que: Los comentarios están moderados y la publicación del comentario podría demorarse hasta ser aprobaddo.

Publicidad

Más publicidad :-)

advising investment services best hyip monitoring rating best hyip program best investment firm best investment opportunity best mutual funds classics investment operator reminiscence stock wiley club estate investment real diversified investment essential of investment estate investment property real estate investment real trust extra income fixed income from investment profit uranium us fundamentals of investment gold investment high capital investments high income high profit fund raising high profit margin high yield investments highest financial returns highest fixed rate hyip invest hyip investment hyip monitoring hyip rating income opportunity investment an introduction investment casting investment club investment company investment land investment portfolio investment property investment putnam investment strategy new hyip offshore investment real estate investment club stock investment guide top mutual funds