Solved

Convertion, YYYYMMDD to DD/MM/YYYY

Posted on 2002-07-12
9
10,834 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 49

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

10 Experts available now in Live!

Get 1:1 Help Now