winform(c#)中如何把textbox中的数据转换成date类型添加到oracle数据库中,谢谢谢了

winform(c#)中如何把textbox中的数据转换成date类型添加到oracle数据库中,谢谢谢了,第1张

DateTime d=new DateTime();

if(DateTimeTryParse(textboxText,out d))

{

string sql =stringFormat(@"insert into 表(时间字段) values('to_date('{0}','yyyy-mm-dd hh24:mi:ss')',d);

}

如果你 *** 作数据库时想通过时间加以限制,那么请以这样的形式存储时间:

year-month-day hour:minute:

second,给一个linux下的存储方法:

void gettime(char name[])

{

时间戳就是如1377216000000 这种格式我们在mysql数据库中会经常用到把时间转换成时间戳或把时间戳转换成日期格式了,下面我来介绍安卓中时间戳 *** 作转换方法。

一、原理

时间戳的原理是把时间格式转为十进制格式,这样就方便时间的计算。好~ 直接进入主题。(下面封装了一个类,有需要的同学可以参考或是直接Copy 就可以用了。)

如: 2013年08月23日 转化后是 1377216000000

二、步骤

1、创建 DateUtilsl类。

代码如下 复制代码

importjavatextParseException;

importjavatextSimpleDateFormat;

importjavautilDate;

/

@author Msquirrel

/

public class DateUtils {

privateSimpleDateFormat sf = null;

/获取系统时间 格式为:"yyyy/MM/dd "/

public static String getCurrentDate() {

Date d = newDate();

sf = newSimpleDateFormat("yyyy年MM月dd日");

returnsfformat(d);

}

/时间戳转换成字符窜/

public static String getDateToString(long time) {

Date d = newDate(time);

sf = newSimpleDateFormat("yyyy年MM月dd日");

returnsfformat(d);

}

/将字符串转为时间戳/

public static long getStringToDate(String time) {

sdf = newSimpleDateFormat("yyyy年MM月dd日");

Date date = newDate();

try{

date = sdfparse(time);

} catch(ParseException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

returndategetTime();

}

2、在对应使用的地方调用就可以了。

代码如下 复制代码

DateUtilsgetCurrentDate(); //获取系统当前时间

DateUtilsgetDateToString(时间戳); //时间戳转为时间格式

DateUtilsgetStringToDate("时间格式");//时间格式转为时间戳

string a = "13:05:30";

DateTime dt = DateTimeParse(a);

MessageBoxShow(dtToString());

或者

TimeSpan ts = TimeSpanParse(a);

MessageBoxShow(tsToString());

可以在C#里面直接转成datetime类型,直接往数据库里插。

13:05:30其实是时间,转datetime是默认转成系统当天日期的13:05:30。

sql server里面有个数据类型Datetime存放的就是日期+时间的格式,比如:1900-1-1 16:36:44000如果要将1900-1-1 16:36:44存入数据库中(即上面的Datetime字段),可以通过转换生成对应的Date数据就行。插入数据库的数据必须是javasqlDate类型的日期,所以你可以通过类似你上面的处理将字符串转成javasqlDate类型的日期再存入数据库就成。下面给你一个将字符串1900-1-1 16:36:44改变成一个javasqlDate类型的一个class。import javatext;import javautilLocale;public class StringToDate {public final static javasqlDate string2Date(String dateString)throws javalangException {DateFormat dateFormat;dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", LocaleENGLISH);dateFormatsetLenient(false);javautilDate timeDate = dateFormatparse(dateString);//util类型javasqlDate dateTime = new javasqlDate(timeDategetTime());//sql类型return dateTime;}}方法二SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");javautilDate now = new javautilDate();String resultDate = sdfformat(now);

up

to

date与to

date的区别和用法

up

to

date与to

date都有

“到目前为止,到现在为止”的意思。表示这种意思的时候,两者可以通用。例如:

up

to

date

(或to

date)

we

have

no

news

of

him

到现在为止我们还不知道他的消息。

to

date

(或up

to

date)

twenty

students

have

been

accepted

into

the

school

到目前为止已有二十名学生被接受入学。

我们通过一下这个句子来看看:

all

our

information

is

kept

right

up

to

date

on

the

computer

我们贮存在计算机中的所有信息都是不断更新的。

本句中的词组up

to

date有两个最主要的意义:一是“切合目前情况的”、“直至目前为止的”;二是“新式的”、“现代化的”。这个词组通常起形容词的作用,一般在句中作表语或补语,定语常用up-to-date的形式。又如:

that

car

is

a

beauty

and

quite

up

to

date

那辆小汽车式样美观、新颖。

he

is

always

right

up

to

date

in

his

information

about

this

subject

在这个学科上,他一向掌握最新的信息。

the

yearly

supplement

keep

the

dictionary

up

to

date

逐年的增补使这部词典的内容能随时跟上形势的发展。

what

he

has

used

is

a

up-to-date

method

of

language

teaching

他使用的是一种最新的语言教学法。

/

Copyright 2014 (C) PANLAB ,All Rights Reserved

/

package comlrlzcommontool;

import javatextParsePosition;

import javatextSimpleDateFormat;

import javautilCalendar;

import javautilDate;

import javautilGregorianCalendar;

/

<p>Title: 基础类</p>

<p>Description: 日期转换</p>

<p>Company: </p>

@version 10

/

public class DateUtils {

/

日期转化为字符串

@param date 时间

@return yyyy-MM-dd HH:mm:ss 格式化的时间字符串

/

public static String dateToString(Date date) {

if(date==null) return "";

return FormatDate(date, "yyyy-MM-dd HH:mm:ss");

}

/

日期转化为字符串

@param date 时间

@return yyyy-MM-dd 格式化的时间字符串

/

public static String dateToStringShort(Date date) {

if(date==null) return "";

return FormatDate(date, "yyyy-MM-dd");

}

/

计算两个日期差(毫秒)

@param date1 时间1

@param date2 时间2

@return 相差毫秒数

/

public static long diffTwoDate(Date date1, Date date2) {

long l1 = date1getTime();

long l2 = date2getTime();

return (l1 - l2);

}

/

计算两个日期差(毫秒)

@param date1 时间1

@param date2 时间2

@return 相差毫秒数

/

public static int diffMinterDate(Date date1, Date date2) {

if(date1==null||date2==null){

return 0;

}

long l1 = date1getTime();

long l2 = date2getTime();

int deff=IntegerparseInt(""+(l1-l2)/1000/60);

return deff;

}

/

计算两个日期差(天)

@param date1 时间1

@param date2 时间2

@return 相差天数

/

public static int diffTwoDateDay(Date date1, Date date2) {

long l1 = date1getTime();

long l2 = date2getTime();

int diff = IntegerparseInt(""+(l1 - l2)/3600/24/1000);

return diff;

}

/

对日期进行格式化

@param date 日期

@param sf 日期格式

@return 字符串

/

public static String FormatDate(Date date, String sf) {

if(date==null) return "";

SimpleDateFormat dateformat = new SimpleDateFormat(sf);

return dateformatformat(date);

}

/

取得当前系统日期

@return yyyy-MM-dd

/

public static String getCurrDate() {

Date date_time = new Date();

return FormatDate(date_time, "yyyy-MM-dd");

}

//取系统时间时一定要用这个方法,否则日期可能不动

public static Date getCurrDateTime(){

return new Date(SystemcurrentTimeMillis());

}

/

返回格式化时间

@param fmt

@return

/

public static String getCurrDateTime(String fmt){

return FormatDate(new Date(SystemcurrentTimeMillis()),fmt);

}

/

取得当前系统时间

@return yyyy-MM-dd HH:mm:ss

/

public static String getCurrTime() {

Date date_time = new Date();

return FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");

}

/

取得日期的天份

@param date 日期

@return dd 天字符串

/

public static String getDay(Date date) {

return FormatDate(date, "dd");

}

/

取得日期的小时

@param date 日期

@return hh 小时字符串

/

public static String getHour(Date date) {

return FormatDate(date, "HH");

}

/

取得日期的分钟

@param date 时间

@return mm 分钟字符串

/

public static String getMinute(Date date) {

return FormatDate(date, "mm");

}

/

取得日期的月份

@param date 日期

@return mm 月份字符串

/

public static String getMonth(Date date) {

return FormatDate(date, "MM");

}

public static int getMonth(Date start, Date end) {

if (startafter(end)) {

Date t = start;

start = end;

end = t;

}

Calendar startCalendar = CalendargetInstance();

startCalendarsetTime(start);

Calendar endCalendar = CalendargetInstance();

endCalendarsetTime(end);

Calendar temp = CalendargetInstance();

tempsetTime(end);

tempadd(CalendarDATE, 1);

int year = endCalendarget(CalendarYEAR)

- startCalendarget(CalendarYEAR);

int month = endCalendarget(CalendarMONTH)

- startCalendarget(CalendarMONTH);

if ((startCalendarget(CalendarDATE) == 1)

&& (tempget(CalendarDATE) == 1)) {

return year 12 + month + 1;

} else if ((startCalendarget(CalendarDATE) != 1)

&& (tempget(CalendarDATE) == 1)) {

return year 12 + month;

} else if ((startCalendarget(CalendarDATE) == 1)

&& (tempget(CalendarDATE) != 1)) {

return year 12 + month;

} else {

return (year 12 + month - 1) < 0 0 : (year 12 + month);

}

}

/

取得时间的秒

@param date 时间

@return ss 秒字符串

/

public static String getSecond(Date date) {

return FormatDate(date, "ss");

}

/

根据年、月取得月末的日期

@param year 年

@parm month 月

@return time 返回日期格式"yyyy-mm-dd"

/

public static String getTime(String year,String month){

String time="";

int len=31;

int iYear=IntegerparseInt(year);

int iMonth=IntegerparseInt(month);

if(iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11)

len=30;

if(iMonth==2){

len=28;

if((iYear%4==0 && iYear%100==0 && iYear%400==0) || (iYear%4==0 && iYear%100!=0)){

len=29;

}

}

time=year+"-"+month+"-"+StringvalueOf(len);

return time;

}

/

取得日期的年份

@param date 日期

@return yyyy 年份字符串

/

public static String getYear(Date date) {

return FormatDate(date, "yyyy");

}

/

字符串转换为日期

@param dateString yyyy-MM-dd HH:mm:ss

@return 日期

/

public static Date stringToDate(String dateString) {

if(dateString==null || dateStringtrim()length()==0) return null;

String datestr = dateStringtrim();

String sf = "yyyy-MM-dd HH:mm:ss";

Date dt = stringToDate(datestr, sf);

if(dt==null) dt = stringToDate(datestr, "yyyy-MM-dd");

if(dt==null) dt = stringToDate(datestr, "MM-dd HH:mm:ss");

if(dt==null) dt = stringToDate(datestr, "dd HH:mm:ss");

if(dt==null) dt = stringToDate(datestr, "yyyyMMdd");

return dt;

}

/ 字符串转换为日期

@param dateString 日期格式字符串

@param sf 日期格式化定义

@return 转换后的日期

/

public static Date stringToDate(String dateString, String sf) {

ParsePosition pos = new ParsePosition(0);

SimpleDateFormat sdf = new SimpleDateFormat(sf);

Date dt = sdfparse(dateString, pos);

return dt;

}

/

字符串转换为日期

@param dateString yyyy-MM-dd

@return 日期

/

public static Date stringToDateShort(String dateString) {

String sf = "yyyy-MM-dd";

Date dt = stringToDate(dateString, sf);

return dt;

}

public DateUtils() {

}

/

获取格式化容器

@param fmt

@return

/

public static SimpleDateFormat getSimFormat(String fmt){

if(StringUtilsisBlank(fmt))fmt=DATE_YMDHMS;

SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);

dateFormatsetLenient(false);

return dateFormat;

}

}

以上就是关于winform(c#)中如何把textbox中的数据转换成date类型添加到oracle数据库中,谢谢谢了全部的内容,包括:winform(c#)中如何把textbox中的数据转换成date类型添加到oracle数据库中,谢谢谢了、strtodate在mysql怎么转换、如何用简单方法把时间戳转换成日期格式等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10144535.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存