注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

网易考拉推荐

设置PDA系统日期时间   

2007-12-19 11:25:11|  分类: Windows Mobile |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

c#例子1

 1 using System;
 2 using System.Runtime.InteropServices;
 3 
 4 class SysDateTime
 5 {
 6     private struct SYSTEMTIME
 7     {
 8         public ushort wYear;
 9         public ushort wMonth;
10         public ushort wDayOfWeek;
11         public ushort wDay;
12         public ushort wHour;
13         public ushort wMinute;
14         public ushort wSecond;
15         public ushort wMilliseconds;
16     }
17 
18     [DllImport("Coredll.dll")]
19     private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);
20 
21     public static void SetDateTime(DateTime dt)
22     {
23         #region Codes==========================================================
24 
25         SYSTEMTIME sysTime = new SYSTEMTIME();
26         
27         sysTime.wYear = Convert.ToUInt16(dt.Year);
28         sysTime.wMonth = Convert.ToUInt16(dt.Month);
29         sysTime.wDay = Convert.ToUInt16(dt.Day);
30         sysTime.wDayOfWeek = Convert.ToUInt16(dt.DayOfWeek);
31         sysTime.wHour = Convert.ToUInt16(dt.Hour);
32         sysTime.wMinute = Convert.ToUInt16(dt.Minute);
33         sysTime.wSecond = Convert.ToUInt16(dt.Second);
34         sysTime.wMilliseconds = Convert.ToUInt16(dt.Millisecond);
35 
36         SetLocalTime(ref sysTime);
37 
38         #endregion
39     }
40 // class

C#例子2:

using System;
using System.Runtime.InteropServices;

namespace setlocaltime
{
    class Systemtime
    {
        [DllImport("coredll.dll")]
        public static extern bool SetLocalTime(ref SYSTEMTIME Time);
        [DllImport("coredll.dll")]
        public static extern void GetLocalTime(ref SYSTEMTIME Time);
    }
    public struct SYSTEMTIME
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;

        /// <summary>
        /// 从System.DateTime转换。
        /// </summary>
        /// <param name="time">System.DateTime类型的时间。</param>
        public void FromDateTime(DateTime time)
        {
            wYear = (ushort)time.Year;
            wMonth = (ushort)time.Month;
            wDayOfWeek = (ushort)time.DayOfWeek;
            wDay = (ushort)time.Day;
            wHour = (ushort)time.Hour;
            wMinute = (ushort)time.Minute;
            wSecond = (ushort)time.Second;
            wMilliseconds = (ushort)time.Millisecond;
        }
        /// <summary>
        /// 转换为System.DateTime类型。
        /// </summary>
        /// <returns></returns>
        public DateTime ToDateTime()
        {
            return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
        }
        /// <summary>
        /// 静态方法。转换为System.DateTime类型。
        /// </summary>
        /// <param name="time">SYSTEMTIME类型的时间。</param>
        /// <returns></returns>
        public static DateTime ToDateTime(SYSTEMTIME time)
        {
            return time.ToDateTime();
        }

    }
}
 

VB.NET做的例子


Imports System
Imports System.Runtime.InteropServices

Public Class Form1
    Private Structure SYSTEMTIME
        Public wYear As UShort
        Public wMonth As UShort
        Public wDayOfWeek As UShort
        Public wDay As UShort
        Public wHour As UShort
        Public wMinute As UShort
        Public wSecond As UShort
        Public wMilliseconds As UShort
    End Structure
    <DllImport("coredll.dll")> _
    Private Shared Function SetLocalTime(ByRef T1 As SYSTEMTIME) As Boolean
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function GetLocalTime(ByRef time As SYSTEMTIME) As Boolean
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim d As SYSTEMTIME
        GetLocalTime(d)
        MessageBox.Show(d.wYear & "-" & d.wMonth & "-" & d.wDay & " " & d.wHour & ":" & d.wMinute & ":" & d.wSecond & " " & d.wDayOfWeek)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim d As SYSTEMTIME
        d.wYear = "2007"
        d.wMonth = "12"
        d.wDay = "18"
        d.wHour = "9"
        d.wMinute = "52"
        d.wSecond = "9"
        SetLocalTime(d)
    End Sub
End Class

将英文的DayOfWeek枚举转化成中文的最简单方式

string [] Day=new string []{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
Console.WriteLine(Day[Convert.ToInt16(DateTime.Now.DayOfWeek)]);

下面这种改法也不错:

const string Day = "日一二三四五六";
Console.WriteLine("星期" + Day[Convert.ToInt16(DateTime.Now.DayOfWeek)]);

备注
在每周有七天的日历中,DayOfWeek 枚举表示一周中的某天。该枚举的范围从零(表示星期日)到六(表示星期六)。

  评论这张
 
阅读(800)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017