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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:23:43+00:00 2026-06-16T22:23:43+00:00

I am getting a date from JSON file, and it is in string format.I

  • 0

I am getting a date from JSON file, and it is in string format.I have two string values of date named startdate and enddate that is coming from intent to currentActivity. Now I want to check that, if date value coming from json file is after the startdate or before the enddate. How can I do this? And yes, the format I json file having is “yyyy-mm-dd”.

JSON Data Look like:

{“posts”:[{“start_date”:”2013-02-15″,”end_date”:”2013-02-21″}]}

Here is the code I have tried but I am getting output as Mon Jan 07 00:00:00 GMT+5:30 2013:

Intent intent = getIntent();
String startDate = intent.getStringExtra("startDate"); //I have check this. It is proper.
String endDate = intent.getStringExtra("endDate"); //I have check this. It is proper.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = null,end = null;
try {
start = sdf.parse(startDate);
end = sdf.parse(endDate);
} catch (ParseException e1) {
e1.printStackTrace();
}

Here is the code of comparing date:

Date date = null;  
try {
    date = sdf.parse(c.getString(TAG_DATE)); //I am getting the date from a json file here.
} catch (ParseException e) {
    e.printStackTrace();
}
Date currDate = new Date();
if (end.compareTo(currDate) < 0 || start.compareTo(currDate) < 0) {
    Toast.makeText(getApplicationContext(),"Please select valid dates...",Toast.LENGTH_SHORT).show();
} else if (end.compareTo(currDate) == 0 && start.compareTo(currDate) >= 0){
    if (date.after(start)) {
        Toast.makeText(getApplicationContext(), "After...",Toast.LENGTH_SHORT).show();
    }
} else if (end.compareTo(currDate) > 0  && start.compareTo(currDate) >= 0) {
        if (date.after(start) && date.before(end)) {
        Toast.makeText(getApplicationContext(),"Before...",Toast.LENGTH_SHORT).show();
    }

}
  • 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-16T22:23:44+00:00Added an answer on June 16, 2026 at 10:23 pm

    I would prefer using a more robust approach that involves converting the date strings to actual Date (or Calendar) objects:

    String startDateString = ...; // get date string from json
    String endDateString = ...; // get date string from json
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date start = formatter.parse(startDateString);
    Date end = formatter.parse(endDateString);
    

    From here on, you can use e.g. start.before(end) or end.after(start) to check whether a date comes before or after another date. If you need more fine-grained control, you can always get the date in milliseconds and have your logic work on that.


    So you’ve updated your code, but you’re leaving it up to us to figure out what’s going on and, more importantly, what you’re expecting to happen? It looks like you want to check how the current date relates to those retrieved from json, although I’m a little lost at what the fourth date field, named date, is for.

    Just some remarks about the current snippet:

    if (end.compareTo(currDate) < 0 || start.compareTo(currDate) < 0)
    

    Personally, I find before() and after() much more readable and descriptive, but I suppose there’s nothing wrong with using compareTo(). However, important to realize is that 0 will only be returned iff the underlying millisecond representations of two dates are equal. With that being said, I’m not sure how much sense it would make to do the first check in the following condition:

    else if (end.compareTo(currDate) == 0 && start.compareTo(currDate) >= 0)
    

    You’ll have to be really lucky to get the milliseconds of end exactly identical to currDate. Unless you do some manipulation somewhere to normalize all the dates to e.g. midnight, it’s unlikely end.compareTo(currDate) == 0 will ever be true.

    Regarding this normalization: have a look at the previously mentioned Calendar class. It’ll allow you to easily retrieve the values for the separate fields of a datestamp/timestamp. For example, if you only want to compare the day, month and year, you can get those specific fields from a Calendar instance with a simple get call. Even more convenient is that you can also set every field independently – that’s great for normalizing all dates to e.g. midnight or midday, after which you can still use the before() and after() methods.

    I’m convinced that should give you enough pointers to correctly code up an implementation that fits your needs. Without exactly knowing what you’re trying to achieve, I’m afraid I can’t help you any further.

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

Sidebar

Related Questions

I'm getting from the server a Json object with this format Date: /Date(1337032800000+0000)/ and
I am getting the date string from the server as 2012-06-21 18:13:33 -04:00 What
We have a from date and To date in Table fields. We are getting
Am I correct in assuming that I have to MANUALLY convert Json-encoded date strings
My application is getting a datetime value from JSON in the following format: Created
I have a json object that I'm loading from wordpress using the JSON API
I am doing Json parsing and retrieving a Date from it. I am getting
I am getting this JSON string from an ASP.Net webservice: {d:{Table:[{col1:123,col2:name,col3:name,col4:100,col5:\/Date(1153033200000)\/},{col1:123,col2:name,col3:name,col4:101,col5:\/Date(1153033200000)\/},{col1:123,col2:name,col3:name,col4:102,col5:\/Date(1153033200000)\/}]}} In my jQuery
I'm getting a date from a Web Service, dd/mm/yyyy hh:mi:ss. Like this: 10/10/2011 12:00:00
I am getting a date from server side C# using the following code: DateTime

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.