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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:43:38+00:00 2026-06-14T08:43:38+00:00

My controller recieves this String 20120115Z as a @RequestParam, representing a date. I would

  • 0

My controller recieves this String “20120115Z” as a @RequestParam, representing a date.

I would like to transform it to this format: yyyy-MM-dd, so I would have a new string like this 2012-01-15.

As you can see, there’s no delimiters, only the ‘Z’ always as the last character.

My approach was pretty obvious:

String data = 
strData.substring(0, 4) + "-" + 
strData.substring(4, 6) + "-" + 
strData.substring(6, 8);

And it works, but as you know these “magic numbers” are something to avoid. I also tried to use a a regular expression like “^[^\s]{4}[^\s]{2}[^\s]{2}Z$”, but without success.

Any idea?

UPDATE: Finally I’ve done it with Joda-Time DateTime class as @Brian Agnew suggested

DateTimeFormatter fmt = DateTimeFormat.forPattern("YYYYMMdd'T'hhmm'Z'");
String strData = "20120115T0600Z";
DateTime dt = fmt.parseDateTime(strData);

printing method:

private static void printDateTime(DateTime dt) {
    int year = dt.getYear();
    int month = dt.getMonthOfYear();
    int day = dt.getDayOfMonth();
    int hour = dt.getHourOfDay();
    int minute = dt.getMinuteOfHour();
    int second = dt.getSecondOfMinute();
    int millis = dt.getMillisOfSecond();
    String zone = dt.getZone().toString();
    log.info("fecha: "
            + day + "/" + month + "/" + year + " " + hour + ":" + minute + ":" + second + ":" + millis
            + " " + zone + "\n");
}

Output

15/1/2012 6:0:0:0 UTC

Thanks everyone

  • 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-14T08:43:40+00:00Added an answer on June 14, 2026 at 8:43 am

    I would perhaps use a Joda-Time DateTimeFormat.

    Why ? It’ll check the format, including valid values for hours/minutes/seconds, and give you back a suitable DateTime object which you can do what with (including reformat it using a similar approach).

    You simply give it the format that you want to parse and it’ll do all the rest. You have a date, so treat it as such.

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

Sidebar

Related Questions

I am working on Play 1.2.4. I have set up the routes like this:
I have this following structure: public class Dummy { public string Name { get;
I have a controller with a postback action: [HttpPost] public ActionResult test(string x) {
If i have a Controller Action that may recieve both HTTP GET and HTTP
Controller: public class HomeController : Controller { Models.MakaleSitesiDBEntities entity = new Models.MakaleSitesiDBEntities(); public ActionResult
Controller First I tried this: [HttpPost] public ActionResult Edit(JournalEntry journalentry) { if (ModelState.IsValid) {
The controller function: function signup() { $bool1 = $this->form_validation->run('username'); $bool2 = $this->form_validation->run('email'); $bool3 =
i am using this to create a new folder System.IO.Directory.CreateDirectory(@ + somevariable); the thing
In my application I have View that observes Model changes. Controller is responsible for
I'm trying to observe a string in a Singleton from a view controller. In

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.