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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:58:51+00:00 2026-06-15T01:58:51+00:00

I get a returned parsed JSON result with string values in the form of

  • 0

I get a returned parsed JSON result with string values in the form of dates like “27-11-2012” which i parse to a date Object. my code for this is:

public Date stringToDateReport(String s){
        //Log.d(TAG,    "StringToDateReport here is " + s);
        DateFormat format;
        Date date = null;

        //if(s.matches(""))
         format = new SimpleDateFormat("dd-MMM-yyyy");

        try {

            date = (Date)format.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

now my issue is, a feature has been implemented that sometimes the json only returns a year object like “2012” and is giving me an “ParseException: Unparseable date” as expected. I was thinking of using regex to match the string pattern and parse from there, but not sure how to do that. Any ideas and also anyway to parse only year in a DateFormat?

  • 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-15T01:58:52+00:00Added an answer on June 15, 2026 at 1:58 am

    I’d try:

    public Date stringToDateReport(String s){
        DateFormat format;
        Date date = null;
    
        format = new SimpleDateFormat("dd-MM-yyyy");
    
        if(s.length()==4) {
            format = new SimpleDateFormat("yyyy");
        }
        try {
            date = (Date)format.parse(s);
        } catch (ParseException e) {
            //you should do a real logging here
            e.printStackTrace();
        }
        return date;
    }
    

    The logic behind is to check if the string is only 4 long, then apply the different format. In this case, this easy method is sufficient, but in other methods, the use of regexes might be required.

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

Sidebar

Related Questions

I'm using Qjson to parse a json object that is returned from a web
I am trying to get some errors returned in JSON format. So, I made
I've got an MVC 3 web app and am returning a JSON object which
I'm trying to parse this string into java, but I keep getting errors. {id:1,jsonrpc:2.0,result:{limits:{end:3,start:0,total:3},sources:[{file:/media/storage/media/re
I am reading a JSON like this: [ { Low: 8.63, Volume: 14211900, Date:
I am trying to send JSON data from a form using the XMLHttpRequest object.
So I am trying to parse some JSON that is returned to me by
Im just starting to get the hang of using json and Id like to
I'm trying to parse the JSON returned from SocialMention. Here's an example of what
I'm trying to get my jQuery to work in this flow: Parse json using

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.