简体   繁体   中英

Date and Calender (input string) conversion and manipulation in Java

The problem I'm trying to solve is as follows. In Nepal we use Bikram Sambat (BS) as our Calendar and also use Georgian Calendar (AD). What I'm trying to do is convert a BS to AD and an AD to BS. BS is 56 years and 8 months ahead of AD. For example today: 17th Feb 2013 is 4 (day) 11 (month) 2070 (year) in BS.

package Day10;

    import java.util.Date;

    /**
     * This is my driver class
     */
    public class Driver {

        public static void main (String[] args){


        DateUtils dateUtils = new DateUtils(); //Object creation

            Date date=dateUtils.getCurrentDate(); //current date


            dateUtils.getAd("10 21 2070"); //method invoke for BS to AD. (It is mm/dd/yyyy BS)


        }
    }

    package Day10;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    /**
     * This is my Service class for converting date values.
     */
    final public class DateUtils {
        public Date getCurrentDate(){

            SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");//date format
            Date date = new Date();//current date
            System.out.println(dateFormat.format(date));

        return date;

        }

        //method to convert BS to AD
        public Calendar getAd (String bs){

            DateFormat dateFormat = new SimpleDateFormat("MM dd yyyy");//date format

            Calendar ad = Calendar.getInstance();

            //Converting String Value to Calendar Object
            try { ad.setTime(dateFormat.parse(bs));

            } catch (ParseException e){

                System.out.println("Invalid");

            }

            System.out.println("Your AD date:"+dateFormat.format(bs));

            return ad;
        }
    }

Problem: While trying to change the String format to Calendar could not determine the variable to store the new converted Calendar value.

I'm trying to change the String value to Calendar and subtract 56 years and 8 months from that. Is that a good possible way?

Thanks.

If you just want to add several years/months to a Calendar, you can
do this as follows. But again, this is not real AD to BS conversion.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;


public class Test040 {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Calendar c = new GregorianCalendar();
        c.add(Calendar.YEAR, 56);
        c.add(Calendar.MONTH, 8);
        c.getTime();
        System.out.println(sdf.format(c.getTime()));
    }

}

When you are parsing a BS String using SimpleDateFormat, it may give parse exception as BS months don't have a same number of dates as AD. Some times a BS month can have 32 days, as anexample, 32-02-2070 is not a valid string. As long as the calculation of number of days in a BS month is implemented in program, I don't think it is possible without any error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM