Java tutorial
package net.sargue.base.util; import org.joda.time.*; import org.joda.time.format.DateTimeFormatter; import static org.joda.time.format.DateTimeFormat.forPattern; import java.sql.*; /** * Copyright 2007,2008,2012 Sergi Baila (sargue@gmail.com) * * This program is free software: you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program. If * not, see <http://www.gnu.org/licenses/>. * * Mtodos auxiliares para la libreria Joda-Time. * * <pre> * Changelog: * 23/08/2007 - primera versin base * 05/09/2007 - formateadores globales (son thread-safe) * 19/03/2008 - nuevos mtodos from*, correccin bug nulls mtodos to* * 03/01/2012 - adaptado para publicar * </pre> */ public class JodaUtils { /* * Formateadores habituales para fechas, horas y fechas con horas. Definidos aqu porque son muy * comunes y al ser thread-safe se pueden reutilizar en varios sitios. */ public final static DateTimeFormatter dateFmt = forPattern("dd/MM/yy"); public final static DateTimeFormatter dtFmt = forPattern("dd/MM/yy HH:mm"); public final static DateTimeFormatter timeFmt = forPattern("HH:mm"); /** * Devuelve la fecha de hoy en forma de string. * * @return la fecha de hoy segn el formato dd/MM/yy */ public static String hoy() { return dateFmt.print(new LocalDate()); } /** * Convierte la fecha en el formato de Joda-Time al equivalente en el formato de JDBC * java.sql.Date * * @param fecha la fecha en formato Joda-Time * @return la misma fecha en formato JDBC */ public static Date toJDBC(LocalDate fecha) { return fecha == null ? null : new Date(fecha.toDateMidnight().getMillis()); } /** * Convierte la hora en el formato de Joda-Time al equivalente en el formato de de JDBC * java.sql.Time * * @param hora la fecha en formato Joda-Time * @return la misma hora en formato JDBC */ public static Time toJDBC(LocalTime hora) { return hora == null ? null : new Time(hora.toDateTimeToday().getMillis()); } /** * Convierte la fecha y hora en el formato de Joda-Time al equivalente en el formato de JDBC * java.sql.Timestamp * * @param dt la fecha en formato Joda-Time * @return la misma fecha en formato JDBC */ public static Timestamp toJDBC(ReadableInstant dt) { return dt == null ? null : new Timestamp(dt.getMillis()); } /** * Convierte la fecha en el formato de JDBC java.sql.Date al equivalente en el formato de * Joda-Time * * @param dt la fecha en formato JDBC * @return la misma fecha en formato Joda-Time */ public static LocalDate fromJDBC(Date fecha) { return fecha == null ? null : new LocalDate(fecha); } /** * Convierte la hora en el formato de JDBC java.sql.Time al equivalente en el formato de Joda-Time * * @param dt la hora en formato JDBC * @return la misma hora en formato Joda-Time */ public static LocalTime fromJDBC(Time hora) { return hora == null ? null : new LocalTime(hora); } /** * Convierte la fecha y hora en el formato de JDBC java.sql.Timestamp al equivalente en el formato * de Joda-Time * * @param dt la fecha y hora en formato JDBC * @return la misma fecha y hora en formato Joda-Time */ public static DateTime fromJDBC(Timestamp ts) { return ts == null ? null : new DateTime(ts); } }