Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:32:59+00:00 2026-06-05T18:32:59+00:00

private String setDate(int day) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.DATE, + day);

  • 0
private String setDate(int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(Calendar.DATE, + day);
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String formattedDate = df.format(cal.getTime());
    return formattedDate;
}

The above method returns “14/06/12” for Locale.UK and “06/14/12” for Locale.US.

How can I get it to return the year in full, i.e. “14/06/2012” for the US locale and “06/14/2012” for UK locale?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:33:00+00:00Added an answer on June 5, 2026 at 6:33 pm

    JHS’s answer is almost complete. The only thing needs to be done is to make it work for all locales.

    import java.text.SimpleDateFormat;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.util.Calendar;
    import java.util.Locale;
    
    class ChangeDateFormat
    {
        private String setDate(int day, Locale locale) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, +day);
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            if (df instanceof SimpleDateFormat)
            {
                SimpleDateFormat sdf = (SimpleDateFormat) df;
                // To show Locale specific short date expression with full year
                String pattern = sdf.toPattern().replaceAll("y+","yyyy");
                sdf.applyPattern(pattern); 
                return sdf.format(cal.getTime());
            }
            String formattedDate = df.format(cal.getTime());
            return formattedDate;
        }
    
        public static void main(String[] args)
        {
            ChangeDateFormat cdf = new ChangeDateFormat();        
            System.out.println(cdf.setDate(1, Locale.JAPAN));
        }
    }
    

    Following Greg’s comments (thanks Greg), here is the relevant note from the javadoc:

    “If you want even more control over the format or parsing, (or want to give your users more control), you can try casting the DateFormat you get from the factory methods to a SimpleDateFormat. This will work for the majority of countries; just remember to put it in a try block in case you encounter an unusual one.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class GalleryDTO implements Parcelable{ private String id; private String title; private String date;
private String kNow(String state, String guess) { for (int i = 0; i <
See the following class public class Parent { private String name; private int age;
private string _itemId; [Browsable(true), Description(Required identifier for the Item.)] public string ItemId { get
private String message = ; public String getMessage() { return message; } public void
This is my code : private string[] MesiSelezionati; protected void Page_Load(object sender, EventArgs e)
[DataContract] public class PersonField { private string _fieldName; private object _fieldValue; public PersonField() {
My code sample is: private String _body; private String _forward; // setup message body
class Person { private: string firstName; string lastName; public: Person() {} Person(ifstream &fin) {
@Entity public class Blobx { private String name; private BlobKey blobKey; @Id @GeneratedValue(strategy =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.