原创

c# 将xx天xx小时xx分钟xx秒转换成秒

此代码是写在c#里的,但是在java也同样适用。
因之前写过将秒转换成xx小时xx分钟xx秒格式的文章,
http://www.sigechuizi.cn/article/128
之前有想写一篇对于将xx天xx小时xx分钟xx秒转换成秒的,最近有点忙,脑子不想思考东西就暂放了,但是正好今天项目用到了,就顺便正好贴到博客中来了
是直接将xx天xx小时xx分钟xx秒类型的数据转换成了秒
参数lastTime 例如 3小时45分钟12秒

public static long formatLongByLastTime(string lastTime)
        {

            string aay = lastTime.Substring(1 + 1, 5 - 1);
            int day = lastTime.IndexOf("天");
            int hours = lastTime.IndexOf("小");
            int minutes = lastTime.IndexOf("分");
            int seconds = lastTime.IndexOf("秒");
            long time = 0;

            if (day > 0)
            {

                long dayLong = long.Parse(lastTime.Substring(0, day));
                time += dayLong * 24 * 3600;
            }
            if (hours > 0)
            {
                if (day > 0)
                {
                    long hoursLong = long.Parse(lastTime.Substring(day + 1, hours - day - 1));
                    time += hoursLong * 3600;
                }
                else
                {

                    long hoursLong = long.Parse(lastTime.Substring(0, hours));
                    time += hoursLong * 3600;
                }

            }
            if (minutes > 0)
            {
                if (hours > 0)
                {
                    long minutesLong = long.Parse(lastTime.Substring(hours + 2, minutes - hours - 2));
                    time += minutesLong * 60;
                }
                else
                {
                    if (day > 0)
                    {
                        long minutesLong = long.Parse(lastTime.Substring(day + 1, minutes - day - 1));
                        time += minutesLong * 60;
                    }
                    else
                    {
                        long minutesLong = long.Parse(lastTime.Substring(0, minutes));
                        time += minutesLong * 60;
                    }
                }

            }
            if (seconds > 0)
            {
                if (minutes > 0)
                {
                    long secondsLong = long.Parse(lastTime.Substring(minutes + 2, seconds - minutes - 2));
                    time += secondsLong;
                }
                else
                {
                    if (hours > 0)
                    {
                        long secondsLong = long.Parse(lastTime.Substring(hours + 2, seconds - hours - 2));
                        time += secondsLong;
                    }
                    else
                    {
                        if (day > 0)
                        {
                            long secondsLong = long.Parse(lastTime.Substring(day + 1, seconds - day - 1));
                            time += secondsLong;
                        }
                        else
                        {
                            long secondsLong = long.Parse(lastTime.Substring(0, seconds));
                            time += secondsLong;
                        }
                    }
                }


            }

            return time ;
        }
正文到此结束
本文目录