mysql 时间转java 字段_java实现时间与字符串之间转换

本文实例为大家分享了java实现时间与字符串之间转换的具体代码,供大家参考,具体内容如下

1. long字符串转换成yyyy-MM-dd HH:mm:ss格式输出

import java.text.SimpleDateFormat;

import java.util.Date;

//将long字符串转换成格式时间输出

public class LongToString {

public static void main(String argsp[]){

String time="1256006105375";

Date date=new Date(Long.parseLong(time));

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

time=formatter.format(date);

System.out.println(time);

}

}

2. 字符串转换成时间

import java.text.SimpleDateFormat;

import java.util.Date;

import ognl.ParseException;

public class StringToDate {

public static void main(String argsp[]) throws Exception{

String time="2010-11-20 11:10:10";

Date date=null;

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

date=formatter.parse(time);

System.out.println(date);

}

}

3. 取得当前系统时间,返回yyyy-MM-dd HH:mm:ss字符串

import java.text.SimpleDateFormat;

import java.util.Date;

public class StringToDate {

public static void main(String argsp[]) throws Exception{

Date date=new Date();

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String time=formatter.format(date);

System.out.println(time);

}

}

4. 取得当前系统时间,返回 HH:mm:ss字符串

import java.text.SimpleDateFormat;

import java.util.Date;

public class StringToDate {

public static void main(String argsp[]) throws Exception{

Date date=new Date();

SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");

String time=formatter.format(date);

System.out.println(time);

}

}

5.将20101125102503转换成2010-11-25 10:25:03输出

import java.text.SimpleDateFormat;

import java.util.Date;

public class StringToDate {

public static void main(String argsp[]) throws Exception{

String time="20101125102503";

SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");

SimpleDateFormat formatter2=new SimpleDateFormat("yyyyHHddHHmmss");

time=formatter1.format(formatter2.parse(time));

System.out.println(time);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的枚举类型可以通过以下步骤将其存储到MySQL中: 1. 在MySQL数据库中创建一个包含枚举常量的列,例如: ``` CREATE TABLE example ( id INT NOT NULL AUTO_INCREMENT, enum_col ENUM('VALUE1', 'VALUE2', 'VALUE3'), PRIMARY KEY (id) ); ``` 2. 在Java中定义一个枚举类型,并添加一个字段来保存枚举常量的值: ``` public enum ExampleEnum { VALUE1("VALUE1"), VALUE2("VALUE2"), VALUE3("VALUE3"); private final String value; ExampleEnum(String value) { this.value = value; } public String getValue() { return value; } } ``` 3. 在Java中使用JDBC将枚举类型转换字符串类型,并将其保存到MySQL中: ``` // 获取数据库连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/example_db", "username", "password"); // 准备SQL语句 String sql = "INSERT INTO example (enum_col) VALUES (?)"; PreparedStatement stmt = conn.prepareStatement(sql); // 将枚举类型转换字符串类型 ExampleEnum exampleEnum = ExampleEnum.VALUE1; String enumValue = exampleEnum.getValue(); // 设置参数并执行SQL语句 stmt.setString(1, enumValue); stmt.executeUpdate(); ``` 4. 在Java中从MySQL中读取枚举类型,并将其转换Java中的枚举类型: ``` // 准备SQL语句 String sql = "SELECT enum_col FROM example WHERE id = ?"; PreparedStatement stmt = conn.prepareStatement(sql); // 设置参数并执行SQL语句 stmt.setInt(1, 1); ResultSet rs = stmt.executeQuery(); // 读取结果集并将其转换Java中的枚举类型 if (rs.next()) { String enumValue = rs.getString("enum_col"); ExampleEnum exampleEnum = ExampleEnum.valueOf(enumValue); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值