?
Solved

Change String format

Posted on 2004-04-20
13
Medium Priority
?
510 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 80 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The Summer 2017 Scholarship Winners have been announced!
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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