2

I need to concat two strings together in Java and then format the string and make it a Date object.

The two strings that I have at the moment are 31/01/2012 and 20:00 and I want to do something like:

try {
  DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
  Date result = new Date();
  String tempDate = date + " " + f;
  result = formatter.parse(tempDate);
} catch (ParseException e) {
  ...etc
}

Can someone help me figure how to add the two strings together. Any help would be appreciated!

SNpn
  • 2,157
  • 8
  • 36
  • 53

3 Answers3

9

Your concatenation is fine. Yet it seems that your format is "dd/MM/yyyy HH:mm"

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

The code looks good. Just correct the format it should be

"dd/MM/yyyy HH:mm"

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
0

tl;dr

LocalDateTime.of( 
    LocalDate.parse( 
        "31/01/2012" , 
        DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
    ) , 
    LocalTime.parse( "20:00" ) 
).atZone( ZoneId.of( "Africa/Tunis" ) ) 

2012-01-31T20:00+01:00[Africa/Tunis]

See this code run live at IdeOne.com.

Using java.time

You are using troublesome old date-time classes, now supplanted by the java.time classes.

You need not concatenate the strings. Parse each, one as a LocalDate and one as a LocalTime.

DateTimeFormatter fDate = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.format( "31/01/2012" , fDate ) ;

LocalTime lt = LocalTime.parse( "20:00" ) ; // Built-in formatter handles standard ISO 8601 formats.

You can combine those two parts into LocalDateTime. A LocalDateTime has no concept of time zone nor offset-from-UTC.

LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;

We have no indication of offset-from-UTC nor time zone. So we do not have a specific moment, on an idea about possible moments. For example, 8 PM comes much earlier in New Zealand (Pacific/Auckland) than it does in India (Asia/Kolkata), and comes hours later in France (Europe/Paris).

To determine a specific moment, we must put that LocalDateTime in the context of a time zone. Perhaps you intended that moment for UTC.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

Or perhaps you meant that for Québec time.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154