| |

VerySource

 Forgot password?
 Register
Search
View: 765|Reply: 3

How to convert time string to numbers

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-18 21:00:02
| Show all posts |Read mode
such as:
String My_date = "2012-11-28 15:25:59";

How to convert
My_month = 11
My_day = 28
...
...
Thank you for that brother's help!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-24 11:15:01
| Show all posts
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String My_date="2012-11-28 15:25:59";
Date date = format.parse(My_date);
Whatever you want, the rest
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-6-24 15:00:01
| Show all posts
Isn't this... quite simple? ?

If the time format is certain, you will find them one by one

Find out that the first four digits are the year, and the sixth and seventh digits are the month... in turn...

Scan the string once it is more complicated. When encountering non-numeric characters, use the previous string as a year or month, etc.

No matter how complicated it is, verify the value

No matter how complicated it is, the date will be automatically modified, for example, 2012-13-11 will automatically become 2013-01-11, etc.

It depends on your needs
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-27 09:15:02
| Show all posts
The stupid way is to decompose the My_date string... If the format in your string is fixed, you can:

int My_year;
int My_day;
int My_month;
String str;
String My_date="2012-11-28 15:25:59";

str=My_date.substring(0,4);
My_year=Integer.parseInt(str);

str=My_date.substring(5,7);
My_month=Integer.parseInt(str);

str=My_date.substring(8,10);
My_day=Integer.parseInt(str);

....
....
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list