Solved

Convertion, YYYYMMDD to DD/MM/YYYY

Posted on 2002-07-12
9
11,042 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
  • 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 50

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 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