0

I have a method that is taking a date and converting it to a long integer.

        Map<String, Object> row = new HashMap<>();
    for (int i = 0; i < columns.size(); i++) {

        String colName = columns.get(i);

        if ("productionDate".equals( colName )) {
            row.put(colName, ((Date) obj[i]).getTime());
        } else {
            row.put(columns.get(i), obj[i]);
        }

I know this line is where it is changing the format, I'm looking for a way to convert it to MM-DD-YYYY

row.put(colName, ((Date) obj[i]).getTime());
2
  • 2
    A format is something you get when you convert it to a string. A date is just a date, it doesn't have a format as such. Commented Feb 11, 2016 at 20:38
  • Sorry, i meant the way the date is displaying now is like: 1437973200000 Commented Feb 11, 2016 at 20:42

3 Answers 3

2

Well, now that you've converted your Date to a long, if you want to display it as a date again in the format you specified you will need to:

1) Get it back to a Date object, i.e.

Date myDate = new Date(mylongvalue);

2) Use SimpleDateFormat to get a String in the format you want from the Date, i.e.

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String formattedDate = sdf.format(myDate);
System.out.println(formattedDate);
Sign up to request clarification or add additional context in comments.

Comments

1

The answer by GriffeyDog and other answers are correct in showing you how to generate a String from a java.util.Date or java.sql.Date object. But…

  • The other Answers are using old outmoded classes.
  • The other Answers fail to address the critical issue of time zone.
  • You may find a better approach in retaining date-time objects rather than storing strings.

java.time

Java 8 and later comes with the java.time framework. These new classes supplant the old java.util.Date/.Calendar and java.text.SimpleTextFormat classes. Those old classes have proven to be confusing, troublesome, and flawed.

From java.sql.Date

If getting a java.sql.Date object from a database (your question is not clear about this), you have date-only value without time-of-day nor time zone. So convert to the java.time.LocalDate class.

LocalDate localDate = myJavaSqlDate.toLocalDate();

From java.util.Date

If getting a java.util.Date object, be aware that is both a date and a time-of-day combined despite the name of the class. Convert to an Instant, a moment on the timeline in UTC.

Instant instant = myJavaUtilDate.toInstant();

Time Zone

From there adjust to a time zone in which you want your date to have a meaning. The date is not the same around the world for any given moment. A new day dawns earlier in the east. So just after midnight in Paris is still ‘yesterday’ in Montréal.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Now use a DateTimeFormatter to generate a String representation of the date value. The pattern codes are slightly different than those of the old java.text.SimpleDateFormat, so read the class doc carefully.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM-dd-uuuu" ); 
String output = zdt.format( formatter );

ISO 8601

If you insist on storing Strings, I suggest changing your format to the standard ISO 8601 format YYYY-MM-DD. Such string values when sorted alphabetically also happen to be chronological. The java.time classes use ISO 8601 formats by default. So no need to specify a coded pattern. Just convert to a LocalDate, then call toString.

String output = zdt.toLocalDate().toString();

Store Date-Time Objects

Better yet, store LocalDate objects as the value in your Map rather than String. Only generate a particular String representation in a particular format when necessary, such as for presentation to a user. Given that LocalDate is now built into Java, you can confidently use it throughout your code knowing it is always available.

1 Comment

Certainly using the java.time package is preferred (if the OP can use Java 8). I was just answering the OP's question as posed.
0

You could try something like:

Date likedDate = new Date(); //Is a made up date row.put (colName.DateFormat.getDateInstance(DateFormat.SHORT).format(likedDated));

SHORT is indicating the format you desire.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.