Home > front end >  Return 2 date variables in the same return?
Return 2 date variables in the same return?

Time:03-31

I have to make a code where, I have to split a string line. The problem is that I need to pass 2 strings, one to dates and the other to times. I can do it, but how do I make the return, return the 2 variables if I don't join them? with " "?

String[] Division = linea.split(",");
        LocalDateTime fecha = Conversion(Division[0],Division[1]); //12/06/2017,12:34

This is the code where I created the date and time but I don't know how to return it,

private static LocalDateTime Conversion(String fechas , String Horas){
        LocalDateTime FechaConvertida =  LocalDateTime.parse(fechas,DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        LocalDateTime HoraConvertida = LocalDateTime.parse(Horas,DateTimeFormatter.ofPattern("HH:mm"));
        return ;

CodePudding user response:

You can return only one object. So, if you need to return more than one object you can use one of these approaches:

  1. Define a class to represent 2 dates:
public class FechaConvertida {
    
    public LocalDateTime FechaConvertida;
    public LocalDateTime HoraConvertida;
    
    public FechaConvertida(LocalDateTime fechaConvertida, LocalDateTime horaConvertida) {
        super();
        FechaConvertida = fechaConvertida;
        HoraConvertida = horaConvertida;
    }    

}

And then return a FechaConvertida instance:

private static FechaConvertida Conversion(String fechas , String Horas){
        LocalDateTime fechaConvertida =  LocalDateTime.parse(fechas,DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        LocalDateTime horaConvertida = LocalDateTime.parse(Horas,DateTimeFormatter.ofPattern("HH:mm"));
        return new FechaConvertida(fechaConvertida, horaConvertida );
  1. Return an array of LocalDateTime:
private static LocalDateTime[] Conversion(String fechas , String Horas){
        LocalDateTime fechaConvertida =  LocalDateTime.parse(fechas,DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        LocalDateTime horaConvertida = LocalDateTime.parse(Horas,DateTimeFormatter.ofPattern("HH:mm"));

        LocalDateTime fechaArray []= {fechaConvertida, horaConvertida}; 
        return fechaArray;
  1. Return a List:
private static Collection<LocalDateTime> Conversion(String fechas , String Horas){
        LocalDateTime fechaConvertida =  LocalDateTime.parse(fechas,DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        LocalDateTime horaConvertida = LocalDateTime.parse(Horas,DateTimeFormatter.ofPattern("HH:mm"));

        Collection<LocalDateTime> dates = new LinkedList<LocalDateTime>();
        dates.add(fechaConvertida)
        dates.add(horaConvertida)
        return dates;

I change the name of the variables in order to lower the case of the first letter cause as a good practice, variables names don't start with a capital letter.

CodePudding user response:

Based on the fact that you're converting a seperate "date" and "time" value, I would use LocalDate and LocalTime to parse the inputs and then combine them into a LocalDateTime result, for example...

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        // Note 12/06/2017 does not match the format of dd-MM-yyyy
        LocalDateTime fecha = Conversion("12-06-2017", "12:34");
        System.out.println(fecha);
    }

    private static LocalDateTime Conversion(String fechas, String Horas) {
        LocalDate FechaConvertida = LocalDate.parse(fechas, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        LocalTime HoraConvertida = LocalTime.parse(Horas, DateTimeFormatter.ofPattern("HH:mm"));
        return LocalDateTime.of(FechaConvertida, HoraConvertida);
    }
}

which prints...

2017-06-12T12:34

CodePudding user response:

You can return a Map of LocalDateTime, see code below as an example:

String[] Division = linea.split(",");
LocalDateTime fecha = doConversion(Division[0],Division[1]).get("fecha");
LocalDateTime hora = doConversion(Division[0],Division[1]).get("hora");


private static Map<LocalDateTime> doConversion(String fechas , String Horas) {
    LocalDateTime fechaConvertida = LocalDateTime.parse(fechas, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
    LocalDateTime horaConvertida = LocalDateTime.parse(Horas, DateTimeFormatter.ofPattern("HH:mm"));

    Map<LocalDateTime> localDateTimeMap = new HashMap<>();
    localDateTimeMap.put("fecha", fechaConvertida);
    localDateTimeMap.put("hora", horaConvertida);

    return localDateTimeMap;
}

btw, in Java, method and variable names start with lower case as Standard (actually it's called camelCase)

  •  Tags:  
  • java
  • Related