@SuppressWarnings("deprecation") public static int daysFromNow(Date data) { Date now = new Date(System.currentTimeMillis()); System.out.println( "now: year = " + (now.getYear() + 1900) + ", month = " + now.getMonth() + ", day = " + now.getDate()); return julianDay(data.getYear(), data.getMonth(), data.getDate()) - julianDay(now.getYear(), now.getMonth(), now.getDate()); }
/** * Trasforma la data in una stringa * * @param data - Data * @return Stringa contenente la data corrente nel seguente formato "5 maggio 2012" */ @SuppressWarnings("deprecation") public String parse(Date data) { int gg = data.getDate(); int mm = data.getMonth(); int aa = data.getYear(); return (gg + " " + month[mm] + " " + aa); }
@Override public String getCalculatedFollowupDateForAjaxCall(String baseDate) { final String empty = ""; String[] elements = baseDate.split("/"); if (elements.length == 3) { try { int month = Integer.parseInt(elements[0]); int day = Integer.parseInt(elements[1]); int year = Integer.parseInt(elements[2]); if (year < 100) { year = year + 2000; } Date requestedDate = new Date(year, month - 1, day - 1); Date followUpDate = getCalculatedFollowupDate(requestedDate); return (followUpDate.getMonth() + 1) + "/" + (followUpDate.getDate() + 1) + "/" + followUpDate.getYear(); } catch (Exception e) { e.printStackTrace(); // something wasn't a number or a valid date element; } } return empty; }
// 用于显示会话时间 public String getTime() { long currentTime = Configs.getReportTime(); Date date1 = new Date(currentTime); Date date2 = new Date(mTimeStamp); SimpleDateFormat format = new SimpleDateFormat("HH:mm"); SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm "); String date = format.format(mTimeStamp); int hour = Integer.parseInt(date.substring(0, 2)); // 今天 if (date1.getDate() - date2.getDate() == 0) { if (hour < 6) return mContext.getString(R.string.before_dawn) + " " + date; else if (hour < 12) return mContext.getString(R.string.morning) + " " + date; else if (hour < 18) return mContext.getString(R.string.afternoon) + " " + date; else return mContext.getString(R.string.night) + " " + date; // 昨天 } else if (date1.getDate() - date2.getDate() == 1) { return mContext.getString(R.string.yesterday); } else if (date1.getDay() - date2.getDay() > 0) { if (date2.getDay() == 1) return mContext.getString(R.string.monday); else if (date2.getDay() == 2) return mContext.getString(R.string.tuesday); else if (date2.getDay() == 3) return mContext.getString(R.string.wednesday); else if (date2.getDay() == 4) return mContext.getString(R.string.thursday); else if (date2.getDay() == 5) return mContext.getString(R.string.friday); else if (date2.getDay() == 6) return mContext.getString(R.string.saturday); else return mContext.getString(R.string.sunday); } else if (date1.getYear() == date2.getYear()) { return date2.getMonth() + 1 + mContext.getString(R.string.month) + date2.getDate() + mContext.getString(R.string.day); } else return format1.format(mTimeStamp); }
/** * Set a parameter to a java.sql.Date value. The driver converts this to a SQL DATE value when it * sends it to the database. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @exception SQLException if a database-access error occurs. */ public void setDate(int parameterIndex, java.sql.Date value) throws SQLException { if (value == null) { setParam(parameterIndex, null, java.sql.Types.DATE, -1); } else { setParam( parameterIndex, new java.sql.Date(value.getYear(), value.getMonth(), value.getDate()), java.sql.Types.DATE, -1); } }
@SuppressWarnings("deprecation") public static String getDateTimeStr(java.sql.Date date, double time) { int year = date.getYear() + 1900; int month = date.getMonth() + 1; int day = date.getDate(); String dateStr = year + "-" + month + "-" + day; Double d = new Double(time); String timeStr = String.valueOf(d.intValue()) + ":00:00"; return dateStr + " " + timeStr; }
@Override public Dialog onCreateDialog(int id) { if (id == 0) { OnDateSetListener callback = new OnDateSetListener() { public void onDateSet(DatePicker view, int year, int month, int day) { date.setYear(year - 1900); date.setMonth(month); date.setDate(day); initializebtnDate(); } }; return new DatePickerDialog( this, callback, date.getYear() + 1900, date.getMonth(), date.getDate()); } else return null; }
@SuppressWarnings("deprecation") @Test public void date() throws SQLException { connectWithJulianDayModeActivated(); Date d1 = new Date(System.currentTimeMillis()); stat.execute("create table t (c1);"); PreparedStatement prep = conn.prepareStatement("insert into t values(?);"); prep.setDate(1, d1); prep.executeUpdate(); ResultSet rs = stat.executeQuery("select c1 from t;"); assertTrue(rs.next()); assertEquals(d1.getYear(), rs.getDate(1).getYear()); assertEquals(d1.getMonth(), rs.getDate(1).getMonth()); assertEquals(d1.getDay(), rs.getDate(1).getDay()); rs.close(); }
public static IntervalXYDataset createDatasetBar() throws Exception { IntervalXYDataset dataset1 = null; Object[][] chart = null; Object[][] chart_qu = null; TCarRecordDao tdao = new TCarRecordDao(); TimeSeries s1, s2; TimeSeriesCollection dataset = new TimeSeriesCollection(); try { if (type == 0) { // 从DAO层获得数据,以天为统计单位,数据线以天为单位 chart = tdao.getCarRecodebyTimearea_day(timefrom, timeto, 1); chart_qu = tdao.getCarRecodebyTimearea_day(timefrom, timeto, 0); s1 = new TimeSeries("入场柱形", Day.class); s2 = new TimeSeries("出场柱形", Day.class); } else { // 传进DAO层的数据是年月,数据线以月的单位 Date date = java.sql.Date.valueOf(timefrom); Date date_t = java.sql.Date.valueOf(timeto); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String newdate_from = sdf.format(date); String timefrom_temp = newdate_from.substring(0, 7); String newdate_to = sdf.format(date_t); String timeto_temp = newdate_to.substring(0, 7); chart = tdao.getCarRecodebyTimearea_month(timefrom_temp, timeto_temp, 1); chart_qu = tdao.getCarRecodebyTimearea_month(timefrom_temp, timeto_temp, 0); s1 = new TimeSeries("入场柱形", Month.class); s2 = new TimeSeries("出场柱形", Month.class); } s1.clear(); Date d, min_r = null, min_c = null, max_r = null, max_c = null; double h; if (chart != null && chart.length > 0) { // 数据为空的处理 System.out.println("ff"); min_r = (Date) chart[0][1]; max_r = (Date) chart[0][1]; for (int i = 0; i < chart.length; i++) { d = (Date) chart[i][1]; if (d.before(min_r)) min_r = d; if (d.after(max_r)) max_r = d; h = new Double(chart[i][0].toString()); // 添加数据,0是以天为单位,1是以月为单位 if (type == 0) s1.add(new Day(d.getDate(), (d.getMonth() + 1), (d.getYear() + 1900)), h); else s1.add(new Month((d.getMonth() + 1), (d.getYear() + 1900)), h); } dataset.addSeries(s1); } else s1 = null; s2.clear(); if (chart_qu != null && chart_qu.length > 0) { min_c = (Date) chart_qu[0][1]; max_c = (Date) chart_qu[0][1]; for (int i = 0; i < chart_qu.length; i++) { d = (Date) chart_qu[i][1]; if (d.before(min_c)) min_c = d; if (d.after(max_c)) max_c = d; h = new Double(chart_qu[i][0].toString()); if (type == 0) s2.add(new Day(d.getDate(), (d.getMonth() + 1), (d.getYear() + 1900)), h); else s2.add(new Month((d.getMonth() + 1), (d.getYear() + 1900)), h); } dataset.addSeries(s2); } else s2 = null; // 取得有数据的时间段的开始和结束 if (min_c != null && min_r != null) min = min_c.before(min_r) ? min_c : min_r; else if (min_c != null) min = min_c; else if (min_r != null) min = min_r; else min = null; if (max_c != null && max_r != null) max = max_c.before(max_r) ? max_r : max_c; else if (max_c != null) max = max_c; else if (max_r != null) max = max_r; else max = null; int seriesCount = dataset.getSeriesCount(); // 一共有多少个序列,目前为一个 for (int i = 0; i < seriesCount; i++) { int itemCount = dataset.getItemCount(i); // 每一个序列有多少个数据项 for (int j = 0; j < itemCount; j++) { if (highValue_Y < dataset.getYValue(i, j)) { // 取第i个序列中的第j个数据项的最大值 highValue_Y = (int) dataset.getYValue(i, j); } if (minValue_Y > dataset.getYValue(i, j)) { // 取第i个序列中的第j个数据项的最小值 minValue_Y = (int) dataset.getYValue(i, j); } } } dataset1 = dataset; return dataset1; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return dataset1; } }
public PrikazIzvjestajaKlijentGUI(Klijent k, Date d1, Date d2) { klijent = k; datum1 = d1; datum2 = d2; JPanel sjeverniPanel = new JPanel(); sjeverniPanel.setLayout(new GridLayout(2, 4, 2, 2)); sjeverniPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); final JLabel naslovLbl = new JLabel(); naslovLbl.setHorizontalAlignment(SwingConstants.RIGHT); naslovLbl.setText("Izvještaj za klijenta: "); java.sql.Date sqld1 = new java.sql.Date(d1.getTime()); java.sql.Date sqld2 = new java.sql.Date(d2.getTime()); JLabel unosLbl = new JLabel(""); unosLbl.setText(k.toString()); JLabel prazna1Lbl = new JLabel(""); JLabel prazna2Lbl = new JLabel(""); JLabel pocetniDatumLbl = new JLabel( "Za period od:" + sqld1.getDate() + "." + (sqld1.getMonth() + 1) + ".20" + (sqld1.getYear() - 100) + "."); pocetniDatumLbl.setHorizontalAlignment(SwingConstants.RIGHT); JLabel pocetniDatumUnosLbl = new JLabel(""); JLabel krajnjiDatumLbl = new JLabel( "do:" + sqld2.getDate() + "." + (sqld2.getMonth() + 1) + ".20" + (sqld2.getYear() - 100) + "."); krajnjiDatumLbl.setHorizontalAlignment(SwingConstants.RIGHT); JLabel krajnjiDatumUnosLbl = new JLabel(""); sjeverniPanel.add(naslovLbl); sjeverniPanel.add(unosLbl); sjeverniPanel.add(prazna1Lbl); sjeverniPanel.add(prazna2Lbl); sjeverniPanel.add(pocetniDatumLbl); sjeverniPanel.add(pocetniDatumUnosLbl); sjeverniPanel.add(krajnjiDatumLbl); sjeverniPanel.add(krajnjiDatumUnosLbl); String imenaKolona[] = {"Radnik", "Vrsta usluge", "Datum", "Broj sati"}; final DefaultTableModel tableModel = new DefaultTableModel(imenaKolona, 0) { @Override public boolean isCellEditable(int row, int column) { return false; } }; try { ik.napuniTabeluKlijent(tableModel, klijent, sqld1, sqld2); } catch (Exception e) { JOptionPane.showMessageDialog( rootPane, "Greška. Pojavio se izuzetak.", "Izuzetak", JOptionPane.ERROR_MESSAGE); System.exit(DISPOSE_ON_CLOSE); } final JTable podaciTbl = new JTable(tableModel); JScrollPane tabelaPane = new JScrollPane(podaciTbl); JPanel juzniPanel = new JPanel(); JButton nazadBtn = new JButton("<< Nazad"); nazadBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); JButton exportBtn = new JButton("Exportuj u .pdf"); exportBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { java.sql.Date sqld1 = new java.sql.Date(datum1.getTime()); java.sql.Date sqld2 = new java.sql.Date(datum2.getTime()); ik.napraviPdfKlijent(klijent, sqld1, sqld2); } catch (Exception ex) { JOptionPane.showMessageDialog( rootPane, "Greška. Pojavio se izuzetak.", "Izuzetak", JOptionPane.ERROR_MESSAGE); System.exit(DISPOSE_ON_CLOSE); } } }); juzniPanel.setBorder(BorderFactory.createEmptyBorder(10, 1, 10, 1)); juzniPanel.setLayout(new GridLayout(1, 3, 3, 3)); juzniPanel.add(nazadBtn); juzniPanel.add(exportBtn); getContentPane().add(sjeverniPanel, BorderLayout.NORTH); getContentPane().add(tabelaPane, BorderLayout.CENTER); getContentPane().add(juzniPanel, BorderLayout.SOUTH); }
/** * @param in Date to convert * @return Day in year */ public static int execute(Date dt) { return execute(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate()); }
// 用于显示消息具体时间 public String getDetailTime() { long currentTime = Configs.getReportTime(); Date date1 = new Date(currentTime); Date date2 = new Date(mTimeStamp); SimpleDateFormat format = new SimpleDateFormat("HH:mm"); String date = format.format(mTimeStamp); SimpleDateFormat format1 = new SimpleDateFormat("yyyy年-MM月-dd日"); String date3 = format1.format(mTimeStamp); int hour = Integer.parseInt(date.substring(0, 2)); if (date1.getDate() - date2.getDate() == 0) { if (hour < 6) return mContext.getString(R.string.before_dawn) + date; else if (hour < 12) return mContext.getString(R.string.morning) + date; else if (hour < 18) return mContext.getString(R.string.afternoon) + date; else return mContext.getString(R.string.night) + date; } else if (date1.getDate() - date2.getDate() == 1) { if (hour < 6) return mContext.getString(R.string.yesterday) + " " + mContext.getString(R.string.before_dawn) + date; else if (hour < 12) return mContext.getString(R.string.yesterday) + " " + mContext.getString(R.string.morning) + date; else if (hour < 18) return mContext.getString(R.string.yesterday) + " " + mContext.getString(R.string.afternoon) + date; else return mContext.getString(R.string.yesterday) + " " + mContext.getString(R.string.night) + date; } else if (date1.getYear() == date2.getYear()) { if (hour < 6) return date2.getMonth() + 1 + mContext.getString(R.string.month) + date2.getDate() + mContext.getString(R.string.day) + " " + mContext.getString(R.string.before_dawn) + date; else if (hour < 12) return date2.getMonth() + 1 + mContext.getString(R.string.month) + date2.getDate() + mContext.getString(R.string.day) + " " + mContext.getString(R.string.morning) + date; else if (hour < 18) return date2.getMonth() + 1 + mContext.getString(R.string.month) + date2.getDate() + mContext.getString(R.string.day) + " " + mContext.getString(R.string.afternoon) + date; else return date2.getMonth() + 1 + mContext.getString(R.string.month) + date2.getDate() + mContext.getString(R.string.day) + " " + mContext.getString(R.string.night) + date; } else if (hour < 6) return date3 + " " + mContext.getString(R.string.before_dawn) + date; else if (hour < 12) return date3 + " " + mContext.getString(R.string.morning) + date; else if (hour < 18) return date3 + " " + mContext.getString(R.string.afternoon) + date; else return date3 + " " + mContext.getString(R.string.night) + date; }