1

I have 4 String parameter's in Java (from request object)

String dd = req.getParameter(dd);
String mm = req.getParameter(mm);
String yyyy = req.getParameter(yyyy);

after validating dd,mm,yyyy I am building date object in 4th String variable by buildingString date = dd+"/"+mm+"/"+yyyy; again in some places i need to replace the date format as following,

date = yyyy+mm+dd;
date =mm+"/"+dd+"/"+yyyy;  // storing in db need this format

How much memory would consume since 4 objects need to pass till persist in table? Was it costlier memory call? Is there any best practice? At last howmany string objects would be in Stringpool?

7
  • 7
    Don't worry about micromanaging objects like this; managed-memory systems like the JVM are extensively optimized to be able to handle these operations efficiently. Write code in the way that is easiest to read. You might, however, consider using a date formatter instead of manually constructing the string. Commented Jul 18, 2015 at 8:24
  • In my point of view, the best practise is use a Calendar/Date. Commented Jul 18, 2015 at 8:25
  • For most usecases you can just go this way. You could use StringBuilder for this too, but since you only create the string once, it won't make a big difference Commented Jul 18, 2015 at 8:25
  • 2
    Put the information into a proper date-representing object as soon as possible, and only create strings (using formats) when there is something to display. In Java 8, use the new java.time package. In earlier versions, use Date and/or Calendar or JodaTime. Do not use strings when saving to database. Commented Jul 18, 2015 at 8:27
  • @RealSkeptic yes, I agree with you while persisting in db i am casting date to Java.util.date object only Commented Jul 18, 2015 at 9:08

1 Answer 1

4

My suggestion is that you should not be worry about micro improvement as other also suggested. May be create a wrapper object e.g. MyDate and construct Date object once. After that you can use different formatter to format your date.

package com.dd;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.LocalDate;

public class MyDate {

    private Date date;

    public MyDate(Request req) {
        int day = req.getParameter("dd");
        int month = req.getParameter("mm");
        int year = req.getParameter("yyyy");
        date = new LocalDate(year, month, day).toDate();
    }

    public String format(String givenFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(givenFormat);
        return sdf.format(date);
    }

}

Note - You need to confirm and validate what values you're getting from your request and if they are in same notation as expected by LocalDate

Sign up to request clarification or add additional context in comments.

1 Comment

You should not be mixing SimpleDateFormat (used with java.util.Date/.Calendar) with Joda-Time. Joda-Time has its own formatter classes.

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.