Solved

Change String format

Posted on 2004-04-20
13
505 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display current date,month,and year to JTextField 3 47
Lamba in java 8 9 239
spring JDBC Template example error 26 206
how to add new optional parameter to JSP 1 27
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

15 Experts available now in Live!

Get 1:1 Help Now