-3

I want to find due date from select date but it does't get. So please help me how to get due date. Here is my code below.

Calendar date = Calendar.getInstance();
date.setTimeInMillis(date_in_mil);
date.add(Calendar.DAY_OF_WEEK_IN_MONTH, 40);

This is for a pregnancy app I am creating.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Please add more information. What is date_in_mil? What happens when you run the code? What result do you expect, and what result do you get? – Modus Tollens Jun 06 '17 at 05:16
  • do you want to get a date after 40 days of date_in_mil? – Sandeep dhiman Jun 06 '17 at 05:21
  • what is due date? Explain it clearly. – Vindhya Pratap Singh Jun 06 '17 at 05:22
  • Is the due date just a date, or a day and a time? – Ole V.V. Jun 06 '17 at 05:24
  • Search and thou shalt find. Similar questions have been asked and answered many times on Stack Overflow. – Ole V.V. Jun 06 '17 at 05:28
  • Possible duplicate of [Adding days to a date in Java](https://stackoverflow.com/questions/12087419/adding-days-to-a-date-in-java) – Ole V.V. Jun 06 '17 at 05:31
  • Vindhya Pratap Singh...i am create pregnancy app so i want to get due date from select date now i hope you can understand – tapan prajapati Jun 06 '17 at 06:04
  • Sandeep dhiman NO i don't want get date after 40 days but i want to get due date from select date – tapan prajapati Jun 06 '17 at 06:06
  • So that should be 40 weeks, not 40 days?? – Ole V.V. Jun 06 '17 at 07:25
  • First off, the `Calendar` class and friends is long outdated. Their better replacement came out with Java 8 in 2014 and has also been backported to Java 6 and 7. I recommend you use `LocalDate` for your purpose. Then the answer is straightforward: `myLocalDate.plusWeeks(40)`. – Ole V.V. Jun 06 '17 at 07:28
  • To use the modern classes like `LocalDate` on Android you need to get [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Jun 06 '17 at 07:30
  • I think much of the confusion comes from “due date” having a very specific meaning when talking pregnancy, but you never mentioned pregnancy in your question, so no one was able to guess that this was what you meant. – Ole V.V. Jun 06 '17 at 08:14
  • Also, you should have explained whether your code worked and if not, what precisely was the difference between expected and actual behaviour. See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) – Ole V.V. Jun 06 '17 at 08:17
  • To convert your time in milliseconds since the epoch to a `LocalDate` in the first place: `LocalDate selectDate = Instant.ofEpochMilli(date_in_mil) .atZone(ZoneId.systemDefault()).toLocalDate();`. – Ole V.V. Jun 06 '17 at 08:25

2 Answers2

1

You are adding 40 to the day of the week in month. This has a different representation as compared to what you actually want. try this instead

date.add(Calendar.DAY_OF_YEAR, 40);

Please read official documentation here


EDIT


here is the complete solution :

String currentDate = "01/05/2017"; 

try { 
  Date curentDateObj = new SimpleDateFormat("dd/mm/yyyy").parse(currentDate); 
  Calendar calendar = Calendar.getInstance(); 
  calendar.setTime(curentDateObj); 
  calendar.add(Calendar.WEEK_OF_YEAR, 40); 
  System.out.println(calendar.getTime()); 
} catch (ParseException e) { 
  e.printStackTrace(); 
} 
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Bawender Yandra
  • 200
  • 1
  • 9
  • ok,but i want to find due date from select date and i was so in due date calculator to count 40 week so – tapan prajapati Jun 06 '17 at 05:26
  • It depends upon what you are trying to add. If its 40 weeks from today that you wish to add then change from Calendar.DAY_OF_YEAR to Calendar.WEEK_OF_YEAR. This will add the week count in a year to +40 weeks from current – Bawender Yandra Jun 06 '17 at 05:32
  • yes, but i got error c.setTimeInMillis(Long.parseLong(current_date)); this line so suggest me what can i do – tapan prajapati Jun 06 '17 at 05:36
  • I also checked online regarding due dates and it says you can add 280days also which is exactly 40 weeks. So in the above example you can change from 40 to 280. date.add(Calendar.DAY_OF_YEAR, 280); – Bawender Yandra Jun 06 '17 at 05:39
  • What is current_date. I am unsure because I cant see your full code. what I would do is create a new Date object and pass long type milliseconds in its constructor and pass this date to Calendar as calendar_obj.setTime(date_obj). – Bawender Yandra Jun 06 '17 at 05:43
  • how can i send you full code ? – tapan prajapati Jun 06 '17 at 05:49
  • just let me know what is current date and i will post a code for the query above. – Bawender Yandra Jun 06 '17 at 05:56
  • ok ,current date select from calander........suppose we select 1/5/2017 and i dont know what is due date....i hope u understand – tapan prajapati Jun 06 '17 at 06:00
  • Ok here is what I can make from your statements. String currentDate = "01/05/2017"; try { Date curentDateObj = new SimpleDateFormat("dd/mm/yyyy").parse(currentDate); Calendar calendar = Calendar.getInstance(); calendar.setTime(curentDateObj); calendar.add(Calendar.WEEK_OF_YEAR, 40); System.out.println(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); } Use this above code and calendar.getTime will give you the due date – Bawender Yandra Jun 06 '17 at 06:33
  • @BawenderYandra, why not [edit your answer](https://stackoverflow.com/posts/44381927/edit) and add the code? It’s *so* hard to read in the comment. – Ole V.V. Jun 06 '17 at 13:00
  • 1
    I have edited the above answer to show the complete answer if anybody wants it in the future. – Bawender Yandra Jun 07 '17 at 03:33
  • ok i will put the code – tapan prajapati Jun 07 '17 at 12:24
1

So you're trying to get 40 days from the day provided in milliseconds?

Calendar date = Calendar.getInstance();
date.setTimeInMillis(date_in_mil);
date.add(Calendar.DAY_OF_YEAR, 40);

Alternatively (from your comments): Calendar date = Calendar.getInstance(); //This is already the current day date.add(Calendar.DAY_OF_YEAR, 40);

Your problem is that you're using DAY_OF_WEEK_IN_MONTH instead of DAY_OF_WEEK

Matthew Bahr
  • 337
  • 1
  • 15