Solved

Change String format

Posted on 2004-04-20
13
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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