• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to change a date in desired format

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a string that contains different numbers that i have broken in different substring and appended as dd/mm/yyyy which denotes a date.

suppose i have a string s=" jan 06,2005"
and i have broken it to sub string jan , 06, 2005.
now i want to give this substring such as jan, 06, 2005 as input and i want to get date according to the geography.
is there any class that we can use so that it can be passed as an input and output will be geographic specific.

suppose we can pass the substring jan , 06,2005 or like dd/mm/yyyy(06/01/2005) but if the geographic area is USA then it will return the string in mm/dd/yyyy(01/06/2005) format.

please send me the code .

I am sending the code what i have below but not getting the desired output.



import java.util.*;
import java.text.*;

public class DateFormating {

static public void displayDate(Locale currentLocale) {
// string anotherday i want to send and the desired output will be according to the geographic area.
String anotherday="jan 05,2005";
Date today;
String result;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat("dd.MM.yy", currentLocale);
today = new Date(anotherday);
result = formatter.format(today);

System.out.println("Locale: " + currentLocale.toString());
System.out.println("Result: " + result);
}


static public void displayPattern(String pattern, Locale currentLocale) {

Date today;
SimpleDateFormat formatter;
String output;

formatter = new SimpleDateFormat(pattern, currentLocale);
today = new Date();
output = formatter.format(today);

System.out.println(pattern + " " + output);
}

static public void main(String[] args) {

Locale[] locales = {
new Locale("fr","FR"),
new Locale("de","DE"),
new Locale("en","IND")
};

for (int i = 0; i < locales.length; i++) {
displayDate(locales[i]);
System.out.println();
}

String[] patterns = {
"dd/MM/yy",
"dd-MM-yy",
"MM/dd/yy",
"MM/dd/yyyy",
"yyyy/mm/dd"

};

for (int k = 0; k < patterns.length; k++) {
//changed the content with in Locale from "en","US" to "en","IND"
displayPattern(patterns[k], new Locale("en","IND"));
System.out.println();
}

System.out.println();
}
}

With Regards
sanjib
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SimpleDateFormat is what you are looking for. The parse() method will convert a String into a Date, and the format() method will do the reverse. The particular date pattern to be used can be supplied to the constructor.
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic