Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convertion, YYYYMMDD to DD/MM/YYYY

Posted on 2002-07-12
9
Medium Priority
?
11,608 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 53

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

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

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
On September 18, Experts Exchange launched the first installment of the Help Bell, a new feature for Premium Members, Team Accounts, and Qualified Experts. The Help Bell will serve as an additional tool to help teams increase question visibility.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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