简体   繁体   English

Java中的日期和日历(输入字符串)转换和操作

[英]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). 在尼泊尔,我们使用Bikram Sambat(BS)作为日历,也使用格鲁吉亚日历(AD)。 What I'm trying to do is convert a BS to AD and an AD to BS. 我正在尝试将BS转换为AD,并将AD转换为BS。 BS is 56 years and 8 months ahead of AD. BS比AD提前56年零8个月。 For example today: 17th Feb 2013 is 4 (day) 11 (month) 2070 (year) in BS. 例如今天:2013年2月17日是BS中的4(天)11(月)2070(年)。

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. 问题:在尝试将字符串格式更改为Calendar时,无法确定用于存储新的转换后Calendar值的变量。

I'm trying to change the String value to Calendar and subtract 56 years and 8 months from that. 我正在尝试将String值更改为Calendar,并从中减去56年零8个月。 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. 但是,这并不是真正的AD到BS的转换。

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. 当您使用SimpleDateFormat解析BS字符串时,它可能会导致解析异常,因为BS月份没有与AD相同的日期数。 Some times a BS month can have 32 days, as anexample, 32-02-2070 is not a valid string. 有时BS月份可以有32天,例如32-02-2070不是有效的字符串。 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. 只要在程序中实现了BS月的天数计算,我认为没有任何错误是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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