Reducir un parcial de Joda-Time

What's the best way to construct a partial from another partial which conains all the necessary fields (e.g. YearMonth desde LocalDate? One way I can see is to convert to a full instant and back, that is

YearMonth ld2ym(LocalDate ld) {
    return new YearMonth(ld.toDateTime(DateTime.now()));
}

but it seems like a more efficient way should be possible.

preguntado el 31 de enero de 12 a las 08:01

Don't really get the DateTime.now() thing. Isn't is easier just to do: LocalDate ld = new LocalDate(); YearMonth ym = new YearMonth(ld); -

@Kennet This is shorter, but will end up doing the same work, so far as I can tell. -

Yes, so your method wolud look like YearMonth ld2ym(LocalDate ld) { return new YearMonth(ld); } So why are you using DateTime.now()? -

@Kennet Because this version makes the inefficiency more obvious. -

1 Respuestas

Sobre el YearMonth class we can find this method which seems to give the appropriate solution:

/**
 * Parses a {@code YearMonth} from the specified string using a formatter.
 * 
 * @param str  the string to parse, not null
 * @param formatter  the formatter to use, not null
 * @since 2.0
 */
public static YearMonth parse(String str, DateTimeFormatter formatter) {
    LocalDate date = formatter.parseLocalDate(str);
    return new YearMonth(date.getYear(), date.getMonthOfYear());
}

Respondido 11 Oct 13, 19:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.