Solved

Change String format

Posted on 2004-04-20
13
507 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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