Solved

Change String format

Posted on 2004-04-20
13
503 Views
Last Modified: 2010-08-05
I have a date  that appears on screen as: 2004/04/20:00:00

So I use out.println(varTime.substring(0, 5)); to strip it down,

I want it show as 20/04/2004 ...how do I change the oder of a sub-String?
0
Comment
Question by:Pigdogmonster
  • 5
  • 4
  • 4
13 Comments
 
LVL 7

Expert Comment

by:searlas
ID: 10867772
out.print(varTime.substring(8, 10));
out.print(varTime.substring(4, 8));
out.println(varTime.substring(0, 4));
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10867830
From where are you getting that Date PigDog? if is ti from DB it will be easier to get it right from the Query

Javier

0
 

Author Comment

by:Pigdogmonster
ID: 10868378
thanks searls but that fails sometimes with:-

StringIndexOutOfBoundsException: String index out of range: 10


The date Is retrived from a database on the previous page and passed to this page through a Query String


String varDelDate = (String)request.getParameter("DelDate");
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10868479
Well the why don't you get it formatted from the DB:

SELECT TO_CHAR(DateField,'DD/MM/YYYY') FROM Table;

That will give it to you on the appropiated way.

Javier

P.S.: Wich DB do you use?
0
 

Author Comment

by:Pigdogmonster
ID: 10868642
SQL database - I change it on the previous page (for display purposes) using:-


while(rs.next()) {
String varDelDate = rs.getString("DelDate");

SimpleDateFormat dbFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
         java.util.Date date = null;
 try
        {
           date = dbFormat.parse(varDelDate);
        }
        catch(java.text.ParseException pe)
        {
           out.println("Failed to parse varDelDate: " + pe.toString());
        }

      SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
       String toDisplay = dateFormat.format(date);


out.println(todisplay);

}

0
 
LVL 7

Accepted Solution

by:
searlas earned 20 total points
ID: 10868662
It would definitely be advisable to find a better way of formatting your date, either by getting the database to do the work as Javier says, or by using a SimpleDateFormat object to format the date, or using Struts etc etc.

But, for the sake of making my code work:
if (varTime != null && varTime.length() >= 10) {
  out.print(varTime.substring(8, 10));
  out.print(varTime.substring(4, 8));
  out.println(varTime.substring(0, 4));
}

This works on the assumption your dates are padded with 0's as in your example: 2004/04/20
If the parameter is not present, or not long enough it simply prints nothing.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Pigdogmonster
ID: 10868969
thanks allot guys!!

Just one quick thing to pick your brains!!

I have a varible that is pulled from the database called: varPreLoaded_Trailer
In the DB columns it shows nulls but the strange thing happens when I  do the following:-

out.println(varPreLoaded_Trailer.length());
//This prints "4"

out.println(varPreLoaded_Trailer);
//This prints "null"

any ideas
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10869018
Is it a Long??
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10869022
or a BLOB?
0
 
LVL 7

Expert Comment

by:searlas
ID: 10869229
It is the string "null"  (four characters long!)

0
 
LVL 6

Expert Comment

by:jarasa
ID: 10869271
That could happend with a BLOB, is 4 bytes long returned always.
Javier
0
 

Author Comment

by:Pigdogmonster
ID: 10869391
how weird!  its a Char 10 and they are all show in the db as <Null>.
0
 
LVL 7

Expert Comment

by:searlas
ID: 10869570
My money's on varPreLoaded_Trailer being the String "null" at that point in the code.

pigdog,

Is varPreLoaded_Trailer declared as a String?
What do you initialize it to?
How do you assign it a value read from the database?

Is the database value really <Null>, or is it actually null (what are you using to check the values in the db?)

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now