Peter Andersson
Peter Andersson

Reputation: 2047

Convert month name to Date range

I need to convert Monthname + Year to a valid date range. It needs to work with leap years etc.

Examples

getDateRange("Feb",2015)  

should find the range 2015-02-01 -- 2015-02-28

While

getDateRange("Feb",2016)  

should find the range 2016-02-01 -- 2016-02-29

Upvotes: 1

Views: 823

Answers (5)

Masudul
Masudul

Reputation: 21961

java.time.temporal.TemporalAdjusters

In Java 8, you can do that using TemporalAdjusters,

LocalDate firstDate= date.with(TemporalAdjusters.firstDayOfMonth());

LocalDate lastDate= date.with(TemporalAdjusters.lastDayOfMonth());

java.time.YearMonth

If you have only year and month, it is better to use YearMonth. From YearMonth you can easily get length of that month.

 YearMonth ym= YearMonth.of(2015, Month.FEBRUARY);
 int monthLen= ym.lengthOfMonth();

Upvotes: 2

Max
Max

Reputation: 2095

Java 8 provides new date API as Masud mentioned.

However if you are not working under a Java 8 environment, then lamma date is a good option.

// assuming you know the year and month already. Because every month starts from 1, there should be any problem to create 
Date fromDt = new Date(2014, 2, 1);  

// build a list containing each date from 2014-02-01 to 2014-02-28
List<Date> dates = Dates.from(fromDt).to(fromDt.lastDayOfMonth()).build();

Upvotes: 0

zutnop
zutnop

Reputation: 611

Java 7 solution with default Java tools:

public static void getDateRange(String shortMonth, int year) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);

    // the parsed date will be the first day of the given month and year
    Date startDate = format.parse(shortMonth + " " + year);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDate);
    // set calendar to the last day of this given month
    calendar.set( Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
    // and get a Date object
    Date endDate = calendar.getTime();

    // do whatever you need to do with your dates, return them in a Pair or print out
    System.out.println(startDate);
    System.out.println(endDate);
}

Upvotes: 1

Puce
Puce

Reputation: 38132

Try (untested):

public List<LocalDate> getDateRange(YearMonth yearMonth){
  List<LocalDate> dateRange = new ArrayList<>();
  IntStream.of(yearMonth.lengthOfMonth()).foreach(day -> dateRange.add(yearMonth.at(day));
  return  dateRange 
}

Upvotes: 0

JuniorDev
JuniorDev

Reputation: 1200

Java 8 made Date-Time operations very simple. For Java 7 and below you could get away with something like this;

void getDate(String month, int year) throws ParseException {
    Date start = null, end = null;

    //init month and year
    SimpleDateFormat sdf = new SimpleDateFormat("MMM");
    Date parse = sdf.parse(month);
    Calendar instance = Calendar.getInstance();
    instance.setTime(parse);
    instance.set(Calendar.YEAR, year);

    //start is default first day of month
    start = instance.getTime();


    //calculate end
    instance.add(Calendar.MONTH, 1);
    instance.add(Calendar.DAY_OF_MONTH, -1);
    end = instance.getTime();

    System.out.println(start + " " + end);
}

The output would be for "Feb", 2015:

Sun Feb 01 00:00:00 EET 2015 
Sat Feb 28 00:00:00 EET 2015

Upvotes: 1

Related Questions