-4

I have a scenario like

"DAY 316 OF YEAR 1998". 

how can i get the exact date and month with the above input? Any ideas?

In Java, how can i achieve this? Please help.

AJJ
  • 3,570
  • 7
  • 43
  • 76
  • 2
    What's `year 98`? Based on what Calendar? – MadProgrammer Aug 09 '13 at 07:44
  • 1
    Looks like a homework. What have you tried? Also, 98 = 1998 or 2098? – Luiggi Mendoza Aug 09 '13 at 07:45
  • 1
    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html – JackTools.Net Aug 09 '13 at 07:48
  • [Here](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) is the API for Java's [Calendar](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) class. Take special note of the constants at the top (one is named `DAY_OF_YEAR`). You can use the _set()_ method to build a calendar with the given year and day of year then read the date from it. – jahroy Aug 09 '13 at 07:52
  • This Question should not have been closed. The day-of-year is a valid concept used in many business and industrial scenarios. We have string of duplicate [Questions on the opposite direction](https://stackoverflow.com/q/3005577/642706), getting the day-of-year from a date, none of which were closed as off-topic. This Question can be answered with simple straight-forward code examples using classes built into Java. – Basil Bourque May 29 '17 at 00:38
  • `Calendar` class is troublesome and now outdated. Use java.time classes instead. `Year( 1998 ).atDay( 316 )`. For just month-day: `MonthDay.from( Year( 1998 ).atDay( 316 ) )` – Basil Bourque May 29 '17 at 00:40

6 Answers6

3

There are any number of ways you might achieve this, based on what you have available.

For example. Based on the String in your example, you could use a SimpleDateFormat to simple parse the String back to a Date object, which is probably the easiest solution I can think of...

try {
  String value = "DAY 316 OF YEAR 1998";
  SimpleDateFormat sdf = new SimpleDateFormat("'DAY' DDD 'OF YEAR' yyyy");
  Date date = sdf.parse(value);

  System.out.println(date);
} catch (ParseException exp) {
  exp.printStackTrace();
}

Once you have a Date object, you can use Calendar to extract various parts of the Date object or simple use another DateFormat to format the value...

Calendar cal = Calendar.getInstance();
cal.setTime(date);

System.out.println("Day of Month = " + cal.get(Calendar.DATE));
// Note, MONTH is 0 indexed...
System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1));

System.out.println("Formatted = " + DateFormat.getDateInstance().format(date));

So you should end up with, something like...

Day of Month = 12
Month = 11
Formatted = 12/11/1998
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2
  1. Parse you string to get two numbers: day of year and year
  2. Create calendar with specified year and 1st day of year as day of year
  3. Set specified day of year
  4. Read date and month parameters from this updated calendar
dbf
  • 6,399
  • 2
  • 38
  • 65
  • *"Parse you string to get two numbers: day of year and year"* ... Why? [`SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) can parse the `String` - FYI- I'm assuming you mean `String` parsing ;) – MadProgrammer Aug 09 '13 at 09:17
2

You should first tell whether the year is 1998, or 2098, or something else.

Apart from that, you can achieve what you want using the Calendar API. The constants of your interest are:

Create a Calendar instance, using Calendar.getInstance(TimeZone). Set the YEAR (1998 or 2098, whatever), and DAY_OF_YEAR(316) using above mentioned constants, and Calendar#set(field, value) method.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

I'm not sure if there is a better way, but what you could try is to use the following code line:

Calendar mycal = new GregorianCalendar(1998, Calendar.JANUARY, 1);
int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH);

This returns the number of days of the month you input. Go from January untill December (increasing/changing your mycal) and substract everything from your daynumber, untill you have a negative number. That would be your month.

Joetjah
  • 6,292
  • 8
  • 55
  • 90
0

Try something like this using the Calender:-

Date d;
Calendar c= Calendar.getInstance();
c.setTime(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Use RegExps to extract the data you need from that string and use the Date/Calendar class to point to that moment. I guess is a homework so I won't give you any code snippets, you must work it out on your own.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43