Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to change a date in desired format

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic