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

  • SEARCH
  • Home
  • 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 6738531
In Process

The Archive Base Latest Questions

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

I need to create a custom deserializer so I can correctly deserialize a date

  • 0

I need to create a custom deserializer so I can correctly deserialize a date that I’m receiving in this format: 2011-10-19T23:30:00-04:00. My Date object is one of many fields, contained one of many nested classes of the Object that I’m deserializing. Everything works fine except for the Date. My class looks like this:

public class OfferContainer{

    public Offer offer;

    public OfferContainer(){}   

    @Override
    public String toString()
    {
        return this.getID() +  offer.toString();
    }

    public String getDescription() {
        return offer.description;
    }

    public String getBusinessName() {
        return offer.business.name;
    }



    public class Offer
    {
        public Category category;
        public String description;
        public String discount;
        public Date expiration;
        public Date published;
        public String rescinded_at;
        public String title;
        public String hook;
        public Date valid_from;
        public Date valid_to;
        public String id;
        public Business business;
        public Location location;
        public String image_270x155;


        @Override
        public String toString()
        {
            return String.format(
                    "[Offer: category=%1$s, description=%2$s, discount=%3$s, expiration=%4$s, published=%5$s, rescinded_at=%6$s, title=%7$s, valid_from=%8$s, valid_to=%9$s, id=%10$s, business=%11$s, location=%12$s]",
                    category, description, discount, expiration, published, rescinded_at, title, valid_from, valid_to, id,
                    business, location);
        }

    }

   public enum Category
    {
        Salon, Spa, Restaurant, Other
    }



    public class Business
    {
        public String name;
        public String phone;
        public Address address;


        @Override
        public String toString()
        {
            return String.format(
                    "[Business: name=%1$s, phone=%2$s, address=%3$s]",
                    name, phone, address);
        }
    }


    public  class Address
    {
        public String address_1;
        public String address_2;
        public String city;
        public String cross_streets;
        public String state;
        public String zip;



        @Override
        public String toString()
        {
            return String.format(
                    "[Address: address_1=%1$s, address_2=%2$s, city=%3$s, cross_streets=%4$s, state=%5$s, zip=%6$s]",
                    address_1, address_2, city, cross_streets, state, zip);
        }
    }

    public class Location {
        public double latitude;
        public double longitude;



        public String toString() {
            return String.format("[Location: longitude=%1$s, latitude=%2$s]", longitude, latitude);
        }

    }

}

How do I use registerTypeAdapter in this scenario and how do I correctly implement a custom deserializer for this?

Thanks in advance!

Also, if it matters, here’s how I’m actually getting my POJO:

  OfferContainer[] offers = gson.fromJson(json, OfferContainer[].class);

Here’s the JSON:

{
    "offer":{
        "category":"Salon",
        "description":"Use this offer now to enjoy this great Salon at a 20% discount. ",
        "discount":"20",
        "expiration":"2011-04-08T02:30:00Z",
        "published":"2011-04-07T12:00:33Z",
        "rescinded_at":null,
        "title":"20% off at Jun Hair Salon",
        "valid_from":"2011-04-07T12:00:31Z",
        "valid_to":"2011-04-08T02:00:00Z",
        "id":"JUN_HAIR_1302177631",
        "business":{
            "name":"Jun Hair Salon",
            "phone":"2126192989",
            "address":{
                "address_1":"12 Mott St",
                "address_2":null,
                "city":"New York",
                "cross_streets":"Chatham Sq & Worth St",
                "state":"NY",
                "zip":"10013"
            }
        },
        "location":{
            "latitude":"40.714021",
            "longitude":"-73.998663"
        },
        "distance":619.8162766744034,
        "draws":[
            "Hair Cut",
            "Blow Dry",
            "Blow Dry Treatment"
        ],
        "claim_link":"http://m-s.com/offers/JUN_HAIR_1302177631?app_id=SavIMg",
        "mobile_claim_link":"http://m-s.com/offers/JUN_HAIR_1302177631?app_id=SavIMg"
    }
}
  • 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-26T11:22:49+00:00Added an answer on May 26, 2026 at 11:22 am

    You need to implement JSONDeserializer<OfferContainer> and then override the deserialize method to achieve this. Have a look at this SO question GSON deserializing key-value to custom object

    You can also try setting the dateformat in the GsonBuilder like this

     Gson gson = new GsonBuilder()
         .setDateFormat("yyyy-MM-dd'T'HHmmss.SSS'Z'")
         .create();
     OfferContainer [] offers = gson.fromJson(json, OfferContainer[].class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a custom token that can fetch a value from a
I need to create a custom volume slider for a WMP object. The current
I need to create a custom constraint annotation which can access the value of
I need to create a custom routing for this url: /par1/par2/par3/par99/11 The url must
I need to create a custom TextBox control that allows user input HTML tags.
Can You tell me about subj? For example I need to create new custom
I need to create a custom Component with JSF 2.0 (not composite component), that
I need to create a custom Contains LINQ expression that derives from System.Linq.Expressions.Expression that
I need to create a custom installer that supports French, German, Spanish, Italian, Polish,
I need to create a custom attribute that is applicable only for non static

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.