473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

convertion of format not working

14
Hi Friends,

I have one due_date variable as string we are declaring . we get the value from service as "2014-02-17T00:00:00:0" LIKE
but, i want the format like '17-FEB-14' as string why because i need to pass this value as string to another method.
how can i acheive it.

i wrote like this.

Expand|Select|Wrap|Line Numbers
  1. StringTokenizer st = new StringTokenizer(due_date, "T");
  2.       String str = st.nextToken();
  3.       SimpleDateFormat formatter5=new SimpleDateFormat("dd-MMM-yyyy");
  4.        due_date2 = formatter5.format(str);

but i got error here, due_date2 = formatter5.format(str);
as can not pass object to date like
Apr 14 '14 #1
3 1209
chaarmann
785 Expert 512MB
You are trying to parse "2014-02-17" with "dd-MMM-yyyy". Of course that is not possible! (Just notice that "17-Feb-2014" would be successfully parsed!) Just read about SimpleDateFormat and the meaning of "dd", "MMM" and "yyyy" and change it so it will parse correctly.
Second step: You don't need a String tokenizer. Just giving the right format string is enough.

To make it clear:
Don't be confused with original format and desired format.
First parse the original string (original format) into a date object
Second print the date object in desired format.
Apr 14 '14 #2
santhu1
14
Hi chaarmann,

thanks for reply i have done some changes it is working fine now.

Expand|Select|Wrap|Line Numbers
  1. StringTokenizer st = new StringTokenizer(due_date, "T");
  2.     String str = st.nextToken();
  3.         DateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd");
  4.  
  5.     DateFormat toFormat = new SimpleDateFormat("dd-MMM-yy");
  6.  
  7.     Date date = fromFormat.parse(str);
  8.     due_date2=toFormat.format(date);
Apr 14 '14 #3
chaarmann
785 Expert 512MB
Nearly perfect. That means, it works, but does unnecessary stuff.
Just remove the StringTokenizer. That means, delete line 1 and 2.
In line 5, use the date-string directly, that means change it to "Date date = fromFormat.parse(due_date);"
Please notice that the parser will ignore all characters given after the parse pattern. That means in your case it will ignore everything after the day "dd", so you do not need to separate the time from the date before by splitting the string.
Apr 16 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Petr Studenovsky | last post by:
Hello I made small GUI test program in Visual Basic NET for SQL communication. I need some help with these problems. problem 1) If I change some settings in datagrid from properties window...
6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
0
by: Tony_E | last post by:
Hello I am trying to export a Crystal Report (8.5) out of an ACCESS (2002) function. crxReport.ExportOptions.FormatType = crEFTExcel80 FlPth = "C:\Reports\CrystalReport" & ".xls"...
0
by: McKoy | last post by:
Hi, I'm translating some code from C++ to C#, but I don't know how to change the code below to get it working under C# ...., I've tried explicity convertion, but without any results :( Please...
4
by: juli | last post by:
Good afternoon! I have an error while trying to convert to DateTime ,here is the code and the error: ArrayList al = new ArrayList(); temp_str=Line.Split(' '); al.Add(temp_str); //...
7
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE...
8
by: Thunder | last post by:
Hi All, I'm using VB.Net 2005. I have for years used Format to format strings and dates, etc... Now when I try to use Format I get the following error? "'Format' is a type and can not be used...
1
by: jagalla | last post by:
Hi, Am Srinivas Jagalla, am getting error Date format. Whatever the format I provided in Local that is comming properly. when I uploaded in live there it isn't comming same format. ex: am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.