Solved

Convertion, YYYYMMDD to DD/MM/YYYY

Posted on 2002-07-12
9
11,126 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 51

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
Industry Leaders: 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

Industry Leaders: 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
excpetion with multiple catch 11 133
Can't access image with tomcat/html 6 106
struts hibernate integration example 28 147
going to wrong jsp page 2 47
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…

756 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