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 6542535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:14:22+00:00 2026-05-25T11:14:22+00:00

A date can be formatted using a e.g. SimpleDateFormat(formatPattern, locale). Is it somehow possible

  • 0

A date can be formatted using a e.g. SimpleDateFormat(formatPattern, locale).
Is it somehow possible to determine the time period (in seconds) that is represented by formatPattern given a date? E.g. if we have

Date date = new Date(1286488800);
String formatPattern = "yyyy";

is it possible to determine the length of the year (in seconds) represented by formatPattern and in which date lies?

  • 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-05-25T11:14:23+00:00Added an answer on May 25, 2026 at 11:14 am

    I believe to have found a proper solution for this. It works for me, although I am not sure and haven’t tested whether this works precisely in every situtions (e.g. with leap seconds). If you have suggestions for improvement, feel free to post them. Here is the code:

    public long getIntervalTimeForFormat(String formatPattern, TimeZone timezone, Locale locale, Date inputDate){
      Date someOddestDate = new Date(1318352124368L);
      GregorianCalendar calendar = new GregorianCalendar();
      calendar.setTime(someOddestDate);
      GregorianCalendar calendarInput = new GregorianCalendar();
      calendarInput.setTime(inputDate);
      Date reducedDate = null;
    try {
        SimpleDateFormat formatter = new SimpleDateFormat(formatPattern, locale);
        formatter.setTimeZone(timezone);
        reducedDate = formatter.parse(formatter.format(someOddestDate));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      GregorianCalendar reducedCalendar = new GregorianCalendar();
      reducedCalendar.setTime(reducedDate);
      int maxField = 0;
      int i = 14;
    //    System.out.println("Reduced date is "+DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN).format(reducedDate)+" - Oddest date is "+DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN).format(someOddestDate));
      while(i > 0 &&  maxField == 0){
    //        System.out.println("Reduced Field "+i+" is set "+reducedCalendar.isSet(i)+" and has value "+reducedCalendar.get(i)+" with actual maximum "+   reducedCalendar.getActualMaximum(i)+" and minimum "+reducedCalendar.getActualMinimum(i)+"-> "+reducedCalendar.getDisplayName(i, DateFormat.FULL, Locale.UK));
    //        System.out.println("Oddest date Field "+i+" is set "+calendar.isSet(i)+" and has value "+calendar.get(i)+" with actual maximum "+ calendar.getActualMaximum(i)+" and minimum "+calendar.getActualMinimum(i)+"-> "+calendar.getDisplayName(i, DateFormat.FULL, Locale.UK));
          if(reducedCalendar.get(i) == calendar.get(i)){
    //            System.out.println("-------> Field "+i+" is equal.");
              maxField = i;
          }
          i--;
      }
      long valueInMillis = Long.MIN_VALUE;
      switch(maxField){
        case 1: valueInMillis = calendarInput.getActualMaximum(6) * 24L * 60 * 60 * 1000; break;// current year granularity
        case 2: valueInMillis = calendarInput.getActualMaximum(5) * 24L * 60 * 60 * 1000; break;// current month granularity
        case 3: //week in month // we just want to know that the granularity is week here and don't care about partial weeks
        case 4: valueInMillis = 7 * 24L * 60 * 60 * 1000; break; // week in year
        case 5: //day granularity
        case 6:
        case 7:
        case 8: valueInMillis = 24L * 60 * 60 * 1000; break; 
        case 9: valueInMillis = 12L * 60 * 60 * 1000; break; //half a day
        case 10: //hour
        case 11: valueInMillis = 60 * 60 * 1000; break; 
        case 12: valueInMillis = 60 * 1000; break; //minute
        case 13: valueInMillis = 1000; break; //second
        case 14: valueInMillis = 1; break; //millisecond
        default: System.err.println("This should never happen.");
      }
    //    System.out.println("Returning "+valueInMillis);
    return valueInMillis;
    

    It basically works by comparing the calender fields of a calendar set to a date that uses all fields (oddestDate) with a calendar set to the same date but formatted, printed, and parsed again by the formatPattern. To compensate at least for leap years, an inputDate is also required.

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

Sidebar

Related Questions

Let's say I have a date that I can represent in a culture-invariant format
Date and time in MySQL can be stored as DATETIME, TIMESTAMP, and INTEGER (number
What C function should I call to obtain a formatted date and time for
Folks, I have a string date which I can convert to numeric using: =DateValue(1/2/2011)
I am trying to convert a string to proper date format using Java's SimpleDateFormat
Given a date how can I add a number of days to it, but
How can I find out the date a MS SQL Server 2000 object was
How can I elegantly print the date in RFC822 format in Perl?
I can't make td Date to have fixed height. If there is less in
How can i map a date from a java object to a database with

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.