首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

MySQL数据类型与优化

1、假如只需要存0~255之间的数,无负数,应使用tinyint unsigned(保证最小数据类型) 2、如果长度不可定,如varchar,应该选择一个你认为不会超过范围的最小类型 比如: varchar(20),可以存20个中文、英文、符号,不要无脑使用varchar(150) 3、整形比字符操作代价更低。比如应该使用MySQL内建的类型(date/time/datetime)而不是字符串来存储日期和时间 4、应该使用整形存储IP地址,而不是字符串 5、尽量避免使用NULL,通常情况下最好指定列为NOT NULL,除非真的要存储NULL值 6、DATETIME和TIMESTAMP列都可以存储相同类型的数据:时间和日期,且精确到秒。然而TIMESTAMP只使用DATETIME一半的内存空间,并且会根据时区变化,具有特殊的自动更新能力。另一方面,TIMESTAMP允许的时间范围要小得多,有时候它的特殊能力会变成障碍

01

java获取当前时间戳转换

package com.pts.peoplehui.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class DateUtils { public static String getTodayDateTime() { SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”, Locale.getDefault()); return format.format(new Date()); } /** * 掉此方法输入所要转换的时间输入例如(”2014年06月14日16时09分00秒”)返回时间戳 * * @param time * @return */ public String data(String time) { SimpleDateFormat sdr = new SimpleDateFormat(“yyyy年MM月dd日HH时mm分ss秒”, Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); String stf = String.valueOf(l); times = stf.substring(0, 10); } catch (Exception e) { e.printStackTrace(); } return times; } public static String getTodayDateTimes() { SimpleDateFormat format = new SimpleDateFormat(“MM月dd日”, Locale.getDefault()); return format.format(new Date()); } /** * 获取当前时间 * * @return */ public static String getCurrentTime_Today() { SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”); return sdf.format(new java.util.Date()); } /** * 调此方法输入所要转换的时间输入例如(”2014-06-14-16-09-00″)返回时间戳 * * @param time * @return */ public static String dataOne(String time) { SimpleDateFormat sdr = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”, Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); String stf = String.valueOf(l); times = stf.substring(0, 10); } catch (Exception e) { e.printStackTrace();

02

获取指定日期的前一天23:59:59

/**  * 获得指定日期的前一天的23:59:59  *  * @param specifiedDay 指定日期  * @return 前一天日期 23:59:59  */ public static Date getSpecifiedDayBefore(Date specifiedDay) {     if (null == specifiedDay) {         return null;     }     Date newDate = null;     try {         Calendar c = Calendar.getInstance();         c.setTime(specifiedDay);         int day = c.get(Calendar.DATE);         c.set(Calendar.DATE, day - 1);         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");         String newDateStr = simpleDateFormat.format(c.getTime()) + " 23:59:59";         SimpleDateFormat newSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         newDate = newSimpleDateFormat.parse(newDateStr);     } catch (ParseException e) {         log.info("日期转换错误" + e.getMessage());     }     return newDate; }

01
领券