Date Format Conversion : Create a simple data file like the example shown below
ID: 3764007 • Letter: D
Question
Date Format Conversion: Create a simple data file like the example shown below containing the 4 dates below plus 10 or more additional dates. The file should include 1 date per line and each date should have the form: Month DayOfTheMonth, Year. All dates should be in this century. No error checking for invalid dates is necessary.
February 19, 2017
July 4, 2017
November 4, 2018
September 30, 2019
.
.
Write a program that will read the dates in the input date file and create an output data file with the form MonthNumber-DayOfTheMonth-Last2DigitsOfTheYear with no extra spaces. Example:
2-19-17
7-4-17
11-4-18
9-30-19
.
.
. .
Explanation / Answer
There are 2 codes. 1.) JavaCode and 2.) Matlab Code
=========================================================
1. JAVA CODE
=========================================================
import java.io.*;
import java.nio.charset.*;
import java.util.StringTokenizer;
class dtfile
{
public static void main(String a[])
{
String line,mon[] = {"January","February","March","April","May","June","July","August","September","Octomber","November","December"},dt_str ="";
int c,month_no = -1,yr;
try {
FileReader fr = new FileReader("asd.txt");
FileWriter fw = new FileWriter("out.txt",true);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line," ,");
c=0;
dt_str="";
while(st.hasMoreTokens())
{
String str = st.nextToken();
if(c == 0)
{
for(int no=0;no<mon.length;no++)
{
if(str.equals(mon[no]))
{
month_no = no+1;
break;
}
}
dt_str = String.valueOf(month_no);
}
if(c == 1)
{
dt_str = dt_str + "-" + str;
}
if(c == 2)
{
yr = Integer.parseInt(str);
yr = yr % 100;
dt_str = dt_str + "-" + yr;
}
c++;
}
fw.write(dt_str +System.getProperty("line.separator"));
}
fw.close();
}
catch(Exception ex){}
}
}
-----------------------------------------------------------------------------------------------------------------------
===========================================================
-----------------------------------------------------------------------------------------------------------------------
2.) Matlab Code
-----------------------------------------------------------------------------------------------------------------------
clc;
clear all;
fid = fopen('Dates_In.txt','r');
fid2 = fopen('Dates_Out.txt','w');
month={'January'; 'February'; 'March'; 'April'; 'May'; 'June'; 'July'; 'August'; 'September'; 'October'; 'November'; 'December'};
ts = textscan(fid, '%s %d8, %d');
len=length(ts{1});
month_out=zeros(1,len);
day_out=zeros(1,len);
year_out=zeros(1,len);
month_in(1:len,1)=ts{1}(1:len);
for i=1:len
for k=1:12
if(strcmp(ts{1}(i),month(k)))
month_out(i)=k;
break;
end
end
end
day_out(1,1:len)=ts{2}(1:len);
year_out(1,1:len)= mod(ts{3}(1:len),100);
for i=1:len
fprintf(fid2,'%d-%d-%d ',month_out(i),day_out(i),year_out(i));
end
fclose(fid);
fclose(fid2);
==============================================================
Sample input for Matlab
---------------------------------
January 31, 2017
September 21, 1947
July 26, 1950
October 2, 1977
April 28, 1982
March 21, 1985
July 1, 2009
February 4, 2014
November 31, 2015
December 01, 2015
January 15, 2016
January 01, 2001
January 01, 1901
December 28, 2102
----------------------------------------------------
Generated Output looks like,
=====================
1-31-17
9-21-47
7-26-50
10-2-77
4-28-82
3-21-85
7-1-9
2-4-14
11-31-15
12-1-15
1-15-16
1-1-1
1-1-1
12-28-2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.