Solved

Convertion, YYYYMMDD to DD/MM/YYYY

Posted on 2002-07-12
9
11,196 Views
Last Modified: 2010-03-05
Can anyone have such expereicen in converting this?
I mean from YYYYMMDD to DD/MM/YYYY or the other way DD/MM/YYYY to YYYYMMDD ?

0
Comment
Question by:foongkim1
[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
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 7151239
In Java?

sure you use java.text.SimpleDateFormat

import java.text.SimpleDateFormat;

String dateStringInOriginalFormat = "20020713";

SimpleDateFormat originalFormatter = new SimpleDateFormat ("yyyyMMdd");
SimpleDateFormat newFormatter = new SimpleDateFormat ("dd/MM/yyyy");

// parsing date string using original format
 ParsePosition pos = new ParsePosition(0);
 Date dateFromString = originalFormatter.parse(dateStringInOriginalFormat, pos);

// Now you have a date object and you can convert it to the new format
String dateStringInNewFormat = newFormatter.format(dateFromString);

To reverse do this:
String dateStringInNewFormat = "13/07/2002";

SimpleDateFormat originalFormatter = new SimpleDateFormat ("yyyyMMdd");
SimpleDateFormat newFormatter = new SimpleDateFormat ("dd/MM/yyyy");

// parsing date string using new format
 ParsePosition pos = new ParsePosition(0);
 Date dateFromString = newFormatter.parse(dateStringInOriginalFormat, pos);

// Now you have a date object and you can convert it to the original format
String dateStringInOriginalFormat = originalFormatter.format(dateFromString);


The only other issue here would be that instead of using Date you may want to write the code using GregorianCalendar b/c most of the Date Object has be deprecated.

But the above should work for most basic needs.

HTH,
CJ
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 7152051
<ping..>
0
 

Author Comment

by:foongkim1
ID: 7154037
cheekjk, my program is in JSP pages... any different is I implement this code?

Actually the DB i used is from DB2 400, so the date type is stored in String format, YYYMMDD, so I nned to display the date in DD/MM/YYYY and convert it to YYYYMMDD. And when I retrieve that time, I need to do the opposite convertion, from YYYMMDD to DD/MM/YYYY.

???
0
Independent Software Vendors: 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 19

Expert Comment

by:cheekycj
ID: 7154595
nope.  Just put the code btw <%... %> tags.

the two code snippets above will do what you need.

if you need specific help, you will have to post your code here.

CJ
0
 

Author Comment

by:foongkim1
ID: 7158561
OK. Cheekycj....

I will try and get back to you.

TQ!!
0
 

Author Comment

by:foongkim1
ID: 7189781
cheekcj,

Yoh.... I am back and I have tried the code but prommpt me this error.

Cheekycj, i think this code will conver the "YYYYMMDD" to "DD/MM/YYYY" right?


*********************
<%@ page language="java" import="java.sql.*, java.io.*, java.util.*, java.text.*, com.wrox.cars.*"
%>
<jsp:useBean id="japf12" class="com.wrox.cars.japf12" scope="session"/>

<html>
<head>
<title>Date Update  </title>
</head>
<body >
 <%
   //This will retrieve TA1023 data field in JavaBean
   // Proved that this data retrieve no problem.
   japf12.connect();
   
   ResultSet rs = japf12.data("BA1013");
   if (rs.next()) {
   
   // declare variable with the data from database
   
   String dateStringInOriginalFormat = rs.getString("TA1023");
   
   SimpleDateFormat originalFormatter = new SimpleDateFormat("yyyyMMdd");
   SimpleDateFormat newFormatter = new SimpleDateFormat("dd/MM/yyy");
   
   ParsePosition pos = new ParsePosition(0);
   Date dateFromString = originalFormatter.parse(dateStringInOriginalFormat, pos);
   
   String dateStringInNewFormat = newFormatter.format(dateFromString);
   
   out.println(dateStringInNewFormat);
     
   }
   japf12.disconnect();
   
 %>


</body>
</html>
**********************



1 error :
" Ambigious class: java.sql.Date. and java.util.Date "
 
** Date dateFromString = originalFormatter.parse(dateStringInOriginalFormat, pos);

1 warning :
java uses or overrides a deprecated API. Recompile with '-deprecration' for details


Any ideas??


And, assume "dateStringInOriginalFormat" is 20021002, then thr output should be 02/10/2001 right?
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 100 total points
ID: 7190766
change it to:
<%@ page language="java" import="java.sql.*, java.io.*, java.util.*, java.text.*, com.wrox.cars.*"
%>
<jsp:useBean id="japf12" class="com.wrox.cars.japf12" scope="session"/>

<html>
<head>
<title>Date Update  </title>
</head>
<body >
<%
  //This will retrieve TA1023 data field in JavaBean
  // Proved that this data retrieve no problem.
  japf12.connect();
 
  ResultSet rs = japf12.data("BA1013");
  if (rs.next()) {
 
  // declare variable with the data from database
 
  String dateStringInOriginalFormat = rs.getString("TA1023");
 
  SimpleDateFormat originalFormatter = new SimpleDateFormat("yyyyMMdd");
  SimpleDateFormat newFormatter = new SimpleDateFormat("dd/MM/yyy");
 
  ParsePosition pos = new ParsePosition(0);
  java.util.Date dateFromString = originalFormatter.parse(dateStringInOriginalFormat, pos);
 
  String dateStringInNewFormat = newFormatter.format(dateFromString);
 
  out.println(dateStringInNewFormat);
   
  }
  japf12.disconnect();
 
%>


</body>
</html>
0
 

Author Comment

by:foongkim1
ID: 7192895
Thanks!!

I sovled my problems.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7193503
Glad I could help and thanx for the "A".

CJ
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
constructor overloading 2 96
SimpleDateFormat.parse examples 19 123
maven project jars 7 62
print map entry 34 79
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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