检查日期在MATLAB中是否有效

时间:2014-09-28 22:44:27

标签: matlab date if-statement switch-statement

为了不误导任何人,这是我试图完成的硬件问题,需要一些帮助。问题本身是非常明显的。我必须写一个函数,我输入一个字符串' Month Day,Year'并让它告诉我它是否有效(真或假的陈述)。我几乎把它都用在了所有的东西上,除非它现在似乎没有认识到我可能几天的约束。

function[valid] = isValidDate(str)
[date, year] = strtok(str, ','); %Should give me the date and year
[~, year2] = strtok(year, ' ');
[month, day] = strtok(date, ' '); %Should give me month and the day
day = round(day);
if length(date) < 6
valid = false;
elseif month(1) == upper(month(1))
    valid = true;
elseif length(date) >= 12
    valid = false;
   end
if year2 >= 0
    valid = true;
else
    valid = false;
end
leapyear = mod(year, 400) == 0 | (mod(year, 4) == 0 ~= mod(year, 100) == 0);

switch month
   case {'September','April','June','November'}

 day <= 30;
    valid = true;
    case {'February'}
        if leapyear
            day <= 29;
            valid = true;
        else
            day <= 28;
            valid = true;
        end
    case {'January', 'March', 'May', 'July', 'August', 'October', 'December'} 
        days <= 31;
        valid = true;
otherwise
    valid = false;

end
end

所以基本上

valid4 = isValidDate('December 29.9, -1005.7')
valid = false

注意:一天之后将没有后缀。我现在唯一的问题是我的功能在几天内没有实现我的约束。它喜欢思考2014年2月30日&#39;是可能的

2 个答案:

答案 0 :(得分:1)

我使用matlab中的java接口制作了一些简单的函数。希望它会有用。

function [valid] = isValidDate(dateStr)
    valid = true;
    dateFormat = java.text.SimpleDateFormat('MMM dd, yyyy');
    dateFormat.setLenient(false);
    try
        dateFormat.parse(dateStr);
        valid = true;
    catch err
        valid = false;
    end       
end

示例:

isValidDate('December 21, 1934');     % gives 1
isValidDate('December 29.9, -1005.7'); % gives 0

答案 1 :(得分:1)

<强>代码

function[valid] = isValidDate(str)

[date1, year1] = strtok(str, ','); %Should give me the date and year
[month1, day1] = strtok(date1, ' '); %Should give me month and the day

%// 1. Take care of bad month strings
all_months = {'January', 'February','March', 'April', 'May','June',...
    'July', 'August', 'September','October','November' 'December'} ;
if ~ismember(cellstr(month1),all_months)
    valid = false;
    return;
end

%// 2. Take care of negative or fraction days
day1 = day1(isstrprop(day1,'digit')); %// Take care of suffixes after day string
num_day = str2double(day1);
if round(num_day)~=num_day || num_day<1
    valid = false;
    return;
end

%// 3. Take care of fraction or negative years
num_year = str2double(strtok(year1,','));
if round(num_year)~=num_year || num_year<0
    valid = false;
    return;
end

lpyr =mod(num_year, 400) == 0 | (mod(num_year, 4) == 0 ~= mod(num_year, 100) == 0);

switch month1
    case {'September','April','June','November'}
        if num_day > 30
            valid = false;
            return;
        end
    case {'February'}
        if (lpyr && num_day > 29) | (~lpyr && num_day > 28)
            valid = false;
            return;
        end
    case {'January', 'March', 'May', 'July', 'August', 'October', 'December'}
        if num_day > 31;
            valid = false;
            return;
        end
end
valid = true; %// We made it through!

return;

如果您更喜欢紧凑的代码 -

function valid = isValidDate(str)

[date1, year1] = strtok(str, ','); %Should give me the date and year
[month1, day1] = strtok(date1, ' '); %Should give me month and the day

%// 1. Take care of bad month strings
all_months = {'January', 'February','March', 'April', 'May','June',...
    'July', 'August', 'September','October','November' 'December'} ;
valid_month = ismember(cellstr(month1),all_months);

%// 2. Take care of negative or fraction days
day1 = day1(isstrprop(day1,'digit')); %// Take care of suffixes after day string
num_day = str2double(day1);
valid_day = round(num_day)==num_day && num_day>=1;

%// 3. Take care of fraction or negative years
num_year = str2double(strtok(year1,','));
valid_year = round(num_year)==num_year && num_year>=0;

%// 4. Take care of valid days based on leap year and days in a month limits
lpyr = mod(num_year, 400) == 0 | (mod(num_year, 4) == 0 ~= mod(num_year, 100) == 0);
valid_leapyear = true;
switch month1
    case {'September','April','June','November'}
        valid_leapyear = num_day<=30;
    case {'February'}
        valid_leapyear = ~((lpyr && num_day>29) || (~lpyr && num_day>28));
    case {'January', 'March', 'May', 'July', 'August', 'October', 'December'}
        valid_leapyear = num_day<=31;
end
valid = valid_year & valid_month & valid_day & valid_leapyear;
return;