datetimeformatter.ofpattern,datetimeformatterofpattern E

http://www.itjxue.com  2023-01-07 09:40  来源:未知  点击次数: 

java 判断一个时间是不是今天的时间范围内

public?class?Test?{

????/**

?????*?java?判断一个时间是不是今天的时间范围内

?????*?@param?args

?????*/

????public?static?void?main(String[]?args)?{

????????String?time?=?"2017-09-27?11:20:45";

????????DateTimeFormatter?dtf?=?DateTimeFormatter.ofPattern("uuuu-MM-dd?HH:mm:ss");

????????LocalDateTime?localTime?=?LocalDateTime.parse(time,?dtf);

????????LocalDateTime?startTime?=?LocalDate.now().atTime(0,?0,?0);

????????LocalDateTime?endTime?=?LocalDate.now().atTime(23,?59,?59);

????????//如果小于今天的开始日期

????????if?(localTime.isBefore(startTime))?{

????????????System.out.println("时间是过去");

????????}

????????//如果大于今天的开始日期,小于今天的结束日期

????????if?(localTime.isAfter(startTime)??localTime.isBefore(endTime))?{

????????????System.out.println("时间是今天");

????????}

????????//如果大于今天的结束日期

????????if?(localTime.isAfter(endTime))?{

????????????System.out.println("时间是未来");

????????}

????}

}

jdk1.8中DateTimeFormatter.ofPattern线程安全问题

你将Date对象和模板对象都做为参数传进静态方法就没有线程安全问题了

关于用dom连续读和写xml 重复换行的问题

public class FlightData { protected Document documentAll = null; protected Document documentUser = null; protected NodeList flightListAll; protected NodeList flightListUser; private int counter; DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter time = DateTimeFormatter.ofPattern("HH:mm"); public FlightData () { counter = 0; DocumentBuilderFactory dbf; DocumentBuilder db; dbf = DocumentBuilderFactory.newInstance(); try { db = dbf.newDocumentBuilder(); documentAll = db.parse("./app/FlightApp/TestData.xml"); flightListAll = documentAll.getElementsByTagName("Flight"); documentUser = db.parse("./app/FlightApp/DataUser.xml"); flightListUser = documentUser.getElementsByTagName("Flight"); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * get the flight object with the index in the xml database * @param the index of the flight that you want to get * @return the flight that matches the index */ private Flight getFlight(NodeList nodelist, int index) { Node flight = nodelist.item(index); NodeList childNodes = flight.getChildNodes(); String flightNumber = childNodes.item(1).getFirstChild().getNodeValue(); String airline = childNodes.item(3).getFirstChild().getNodeValue(); String airplaneType = childNodes.item(5).getFirstChild().getNodeValue(); String terminal = childNodes.item(7).getFirstChild().getNodeValue(); String gate = childNodes.item(9).getFirstChild().getNodeValue(); String departureAirport = childNodes.item(11).getFirstChild().getNodeValue(); String arrivalAirport = childNodes.item(13).getFirstChild().getNodeValue(); String departureDateTime = childNodes.item(15).getFirstChild().getNodeValue(); String arrivalDateTime = childNodes.item(17).getFirstChild().getNodeValue(); String seat = childNodes.item(19).getFirstChild().getNodeValue(); Flight f = new Flight(flightNumber, airline, airplaneType, terminal, gate, departureAirport, arrivalAirport, seat, departureDateTime, arrivalDateTime);// System.out.println(flightNumber);// System.out.println(airline);// System.out.println(airplaneType);// System.out.println(terminal);// System.out.println(gate);// System.out.println(departureAirport);// System.out.println(arrivalAirport);// System.out.println(departureDateTime);// System.out.println(arrivalDateTime);// System.out.println(seat); return f; } /** * get the all the flight * @return all the flights in a List */ public ListFlight getFlightsAll() { return getFlights(flightListAll); } /** * get the user's flight * @return the user' flights in a List */ public ListFlight getFlightsUser() { return getFlights(flightListUser); } private ListFlight getFlights(NodeList nodelist) { ListFlight flights = new ArrayListFlight(); for (int i = 0; i nodelist.getLength(); i++) { flights.add(getFlight(nodelist, i)); } return flights; } /** * @return the length of the list of all flights */ public int getSizeAllFlights () { return flightListAll.getLength(); } /** * @return the length of the list of user's flights */ public int getSizeUserFlights () { return flightListUser.getLength(); } public void addNodeToUserFlights() { //TODO delete Flight a = new Flight("ABC2", "Lufthansa", "A320", "2", "C", "FAO", "MUC", "13A", "2018-05-11 18:00", "2018-05-11 19:00"); addNode("./app/FlightApp/DataUser.xml", a, "Flights", documentUser); } private void addNode(String fileName, Flight flight, String fatherNodeName, Document document) { assert(document.equals(flightListUser)); try { Element newflight = document.createElement("Flight"); counter++; newflight.setAttribute("id", getSizeUserFlights() + counter + ""); Element flightNumber = document.createElement("flightNumber"); Element airline = document.createElement("airline"); Element airplaneType = document.createElement("airplaneType"); Element terminal = document.createElement("terminal"); Element gate = document.createElement("gate"); Element departureAirport = document.createElement("departureAirport"); Element arrivalAirport = document.createElement("arrivalAirport"); Element departureDateTime = document.createElement("departureDateTime"); Element arrivalDateTime = document.createElement("arrivalDateTime"); Element seat = document.createElement("seat"); flightNumber.setTextContent(flight.getFlightNumber()); airline.setTextContent(flight.getAirline()); airplaneType.setTextContent(flight.getAirplaneType()); terminal.setTextContent(flight.getTerminal()); gate.setTextContent(flight.getGate()); departureAirport.setTextContent(flight.getDepartureAirport()); arrivalAirport.setTextContent(flight.getArrivalAirport()); departureDateTime.setTextContent(flight.getDepartureDateTime().format(date) + " " + flight.getDepartureDateTime().format(time)); arrivalDateTime.setTextContent(flight.getArrivalDateTime().format(date) + " " + flight.getArrivalDateTime().format(time)); seat.setTextContent(flight.getSeat()); newflight.appendChild(flightNumber); newflight.appendChild(airline); newflight.appendChild(airplaneType); newflight.appendChild(terminal); newflight.appendChild(gate); newflight.appendChild(departureAirport); newflight.appendChild(arrivalAirport); newflight.appendChild(departureDateTime); newflight.appendChild(arrivalDateTime); newflight.appendChild(seat); Element fatherElement = (Element)document.getElementsByTagName(fatherNodeName).item(0); fatherElement.appendChild(newflight); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{}indent-amount", "2"); StreamResult result = new StreamResult(new FileOutputStream(fileName)); transformer.transform(source, result); } catch(Exception e) { System.out.println(e.getMessage()); } }

Java 日期如何判断是星期几?求大神代码

public static void main(String[] agrs) {

String newtime="2013-7-21";

System.out.print(testDate(newtime));

}

public static String testDate(String newtime) {

String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

Calendar c = Calendar.getInstance();// 获得一个日历的实例

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

try {

c.setTime(sdf.parse(newtime));

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return dayNames[c.get(Calendar.DAY_OF_WEEK)-1];

}

对于单双数日,能被2整除就是双数日,反之是单数日

在Java中,以下哪种定义,可以得到4位年2位月2位日,24小时制的格式()(2分?)?

LocalDateTime dateTime = LocalDateTime.now();

String str = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

System.out.println(str);

09-01-2022如何转化成2022/1/9

英国表示日期两种方式:月日年,日月年,所以此日期可以理解为一月九日或者九月一日。美国则只有一种方式月日年,则为九月一日。咱们中国为年月日!!!呵呵呵!!!!

(责任编辑:IT教学网)

更多