我的问题是标题。
DateTime类
import java.util.Date;
class DateTime {
private static long advance; // keeps tracks of any time advance
private long time;
public DateTime() // constructor
{
time = System.currentTimeMillis() + advance;
}
public long getTime() {
return time;
}
// advances date/time by specified days, hours and mins for testing purpose
public static void setAdvance(int days, int hours, int mins) {
advance = ((days * 24L + hours) * 60L) * 60000L;
}
public String toString() {
long l = getTime();
Date gct = new Date(l);
return gct.toString();
}
public static String getCurrentTime() // returns current date/time
{
Date d = new Date(System.currentTimeMillis() + advance);
return d.toString();
}
// returns difference in days
public static int diffDays(DateTime d2, DateTime d1) {
return (int) (1 + (d2.getTime() - d1.getTime()) / (24L * 60 * 60 * 1000));
}
}
Vehicle class
public class Vehicle {
public enum State { A, S, H}
protected State status;
protected int odo;
protected double dailyRate;
protected DateTime hiredDate;
protected String vehicleID, hirerID, description;
public Vehicle (String vehicleID, String description, double dailyRate, int odo) {
this.vehicleID = vehicleID;
this.description = description;
this.dailyRate = dailyRate;
this.odo = odo;
status = State.A;
}
}
我将通过
存储为csv文件格式vehicleID,desription,dailyRate,odo,hirerID,hiredDate
这是我的readFile()
方法,我将从csv中读取并将其放回
BufferedReader br = null;
String line = "";
String spliter = ",";
Vehicle.State status;
String vehicleID, description, hirerID, cID, cName, cPhone;
DateTime hiredDate;
double dailyRate = 0.0, discountRate = 0.0;
int odo = 0, freeMilAllowance = 0, serviceLength = 0,
odoFromLastService = 0, pastMileage = 0;
try {
br = new BufferedReader(new FileReader(filePath));
while ((line = br.readLine()) != null) {
if (fileType == Type.V) {
vehicleID = file[0];
description = file[1];
try {
dailyRate = Double.parseDouble(file[2]);
} catch (NumberFormatException nfe) {
System.err.println(nfe);
}
try {
odo = Integer.parseInt(file[3]);
} catch (NumberFormatException nfe) {
System.err.println(nfe);
}
status = Vehicle.State.valueOf(file[4]);
hirerID = file[5];
hiredDate = file[6]
// Construct a vehicle and add into list
Vehicle v = new Vehicle (vehicleID, description, dailyRate, odo);
v.setStatus(status);
v.setHirerID(hirerID);
v.setHiredDate(hireDate);
vehicleList.add(v);
如何从String转换并将其解析为DateTime
对象,以便我可以在我的程序中使用?
答案 0 :(得分:1)
如何从String转换并将其解析为DateTime对象,以便我可以在我的程序中使用?
我不是100%确定要转换的列中的值。我假设它是某种日期时间字符串。我倾向于使用Jodatime class SynchroClient
{
public function __construct()
{
}
public function synchronize () {
$ref_db = $this->getDoctrine()->getManager();
$other_db = $this->getDoctrine()->getManager('other');
$sql = ' SELECT active, .... ';
$statement = $other_db->getConnection()->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
foreach ($result as $entity) {
$client = new Client();
$client->setActive($entity['active']);
$ref_db->persist($client);
}
$sql_truncate = ' TRUNCATE TABLE client';
$statement = $ref_db->getConnection()->prepare($sql_truncate);
$statement->execute();
$ref_db->flush();
return new Response('1');
}
}
。例如:
DateTimeFormat
这会生成一个DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/YYYY");
DateTime current = formatter.parseDateTime(valueString);
对象,不应与您的org.joda.time.DateTime
混淆。然后,您可以调用DateTime
以获取自纪元以来的毫秒数。
另外,要解析CSV文件,可能需要使用像SimpleCSV这样的CSV库。还有很多其他的。
答案 1 :(得分:0)
以下是您可以阅读的示例:https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/
假设格式为:vehicleID,desription,dailyRate,odo,hirerID,hiredDate
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVReader {
public static void main(String[] args) {
String csvFile = "/.../csv/vehicle.csv";
String line = "";
String cvsSplitBy = ",";
DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/YYYY");
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] vehicle = line.split(cvsSplitBy);
System.out.println("Vehicle [id= " + vehicle[0] + " ,
..., +"hiredDate=" + vehicle[5] + "]");
// And here get data
DateTime dd = formatter.parseDateTime(vehicle[5]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
并写
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.csvreader.CsvWriter;
public class CsvWriterAppendExample {
public static void main(String[] args) {
String outputFile = "vehicle.csv";
// before we open the file check to see if it already exists
boolean alreadyExists = new File(outputFile).exists();
try {
// use FileWriter constructor that specifies open for appending
CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
// if the file didn't already exist then we need to write out the header line
if (!alreadyExists)
{
csvOutput.write("vehicleID");
csvOutput.write("desription");
csvOutput.write("dailyRate");
csvOutput.write("odo");
csvOutput.write("hirerID");
csvOutput.write("hiredDate");
csvOutput.endRecord();
}
// else assume that the file already has the correct header line
// write out a few records
csvOutput.write("any vehicleID");
csvOutput.write("... Bruce");
csvOutput.write("12");
csvOutput.write("dailyRate");
csvOutput.write("od");
csvOutput.write("hiererID");
csvOutput.write("data");
csvOutput.endRecord();
csvOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}